Skip to content

Commit

Permalink
feat(errors): include completion in LengthFinishReasonError (#1701)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored Sep 10, 2024
1 parent 6b07089 commit b0e3256
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
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

0 comments on commit b0e3256

Please sign in to comment.