Skip to content

feat(errors): include completion in LengthFinishReasonError #1701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/openai/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

from __future__ import annotations

from typing import Any, Optional, cast
from typing import TYPE_CHECKING, Any, Optional, cast
from typing_extensions import Literal

import httpx

from ._utils import is_dict
from ._models import construct_type

if TYPE_CHECKING:
from .types.chat import ChatCompletion

__all__ = [
"BadRequestError",
"AuthenticationError",
Expand Down Expand Up @@ -130,10 +133,20 @@ class InternalServerError(APIStatusError):


class LengthFinishReasonError(OpenAIError):
def __init__(self) -> None:
super().__init__(
f"Could not parse response content as the length limit was reached",
)
completion: ChatCompletion
"""The completion that caused this error.

Note: this will *not* be a complete `ChatCompletion` object when streaming as `usage`
will not be included.
"""

def __init__(self, *, completion: ChatCompletion) -> None:
msg = "Could not parse response content as the length limit was reached"
if completion.usage:
msg += f" - {completion.usage}"

super().__init__(msg)
self.completion = completion


class ContentFilterFinishReasonError(OpenAIError):
Expand Down
2 changes: 1 addition & 1 deletion src/openai/lib/_parsing/_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def parse_chat_completion(
choices: list[ParsedChoice[ResponseFormatT]] = []
for choice in chat_completion.choices:
if choice.finish_reason == "length":
raise LengthFinishReasonError()
raise LengthFinishReasonError(completion=chat_completion)

if choice.finish_reason == "content_filter":
raise ContentFilterFinishReasonError()
Expand Down
4 changes: 3 additions & 1 deletion src/openai/lib/streaming/chat/_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ def _accumulate_chunk(self, chunk: ChatCompletionChunk) -> ParsedChatCompletionS

if has_parseable_input(response_format=self._response_format, input_tools=self._input_tools):
if choice.finish_reason == "length":
raise LengthFinishReasonError()
# at the time of writing, `.usage` will always be `None` but
# we include it here in case that is changed in the future
raise LengthFinishReasonError(completion=completion_snapshot)

if choice.finish_reason == "content_filter":
raise ContentFilterFinishReasonError()
Expand Down