Skip to content

Commit 7c6e413

Browse files
committed
feat(errors): include usage details in LengthFinishReasonError
1 parent 6b07089 commit 7c6e413

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Diff for: src/openai/_exceptions.py

+17-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 import CompletionUsage
15+
1316
__all__ = [
1417
"BadRequestError",
1518
"AuthenticationError",
@@ -130,10 +133,19 @@ 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+
usage: CompletionUsage | None
137+
"""The usage details for the completion that caused this error.
138+
139+
Note: this will *not* be set when streaming.
140+
"""
141+
142+
def __init__(self, *, usage: CompletionUsage | None) -> None:
143+
msg = "Could not parse response content as the length limit was reached"
144+
if usage:
145+
msg += f" - {usage}"
146+
147+
super().__init__(msg)
148+
self.usage = usage
137149

138150

139151
class ContentFilterFinishReasonError(OpenAIError):

Diff for: 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(usage=chat_completion.usage)
7373

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

Diff for: 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(usage=completion_snapshot.usage)
398400

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

0 commit comments

Comments
 (0)