Skip to content

Commit ee39217

Browse files
RobertCraigiestainless-app[bot]
authored andcommitted
feat(errors): include completion in LengthFinishReasonError (openai#1701)
1 parent f74a778 commit ee39217

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/openai/_exceptions.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
from __future__ import annotations
44

5-
from typing import Any, Optional, cast
5+
from typing import TYPE_CHECKING, Any, Optional, cast
66
from typing_extensions import Literal
77

88
import httpx
99

1010
from ._utils import is_dict
1111
from ._models import construct_type
1212

13+
if TYPE_CHECKING:
14+
from .types.chat import ChatCompletion
15+
1316
__all__ = [
1417
"BadRequestError",
1518
"AuthenticationError",
@@ -130,10 +133,20 @@ class InternalServerError(APIStatusError):
130133

131134

132135
class LengthFinishReasonError(OpenAIError):
133-
def __init__(self) -> None:
134-
super().__init__(
135-
f"Could not parse response content as the length limit was reached",
136-
)
136+
completion: ChatCompletion
137+
"""The completion that caused this error.
138+
139+
Note: this will *not* be a complete `ChatCompletion` object when streaming as `usage`
140+
will not be included.
141+
"""
142+
143+
def __init__(self, *, completion: ChatCompletion) -> None:
144+
msg = "Could not parse response content as the length limit was reached"
145+
if completion.usage:
146+
msg += f" - {completion.usage}"
147+
148+
super().__init__(msg)
149+
self.completion = completion
137150

138151

139152
class ContentFilterFinishReasonError(OpenAIError):

src/openai/lib/_parsing/_completions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def parse_chat_completion(
6969
choices: list[ParsedChoice[ResponseFormatT]] = []
7070
for choice in chat_completion.choices:
7171
if choice.finish_reason == "length":
72-
raise LengthFinishReasonError()
72+
raise LengthFinishReasonError(completion=chat_completion)
7373

7474
if choice.finish_reason == "content_filter":
7575
raise ContentFilterFinishReasonError()

src/openai/lib/streaming/chat/_completions.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ def _accumulate_chunk(self, chunk: ChatCompletionChunk) -> ParsedChatCompletionS
394394

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

399401
if choice.finish_reason == "content_filter":
400402
raise ContentFilterFinishReasonError()

0 commit comments

Comments
 (0)