Skip to content

Commit 4725329

Browse files
authored
Handle errors in OpenAI response (#910)
1 parent 06b5531 commit 4725329

File tree

1 file changed

+4
-2
lines changed
  • logfire/_internal/integrations/llm_providers

1 file changed

+4
-2
lines changed

logfire/_internal/integrations/llm_providers/openai.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from openai.types.create_embedding_response import CreateEmbeddingResponse
1111
from openai.types.images_response import ImagesResponse
1212

13+
from ...utils import handle_internal_errors
1314
from .types import EndpointConfig, StreamState
1415

1516
if TYPE_CHECKING:
@@ -113,18 +114,19 @@ def get_response_data(self) -> Any:
113114
OpenaiChatCompletionStreamState = OpenaiCompletionStreamState # type: ignore
114115

115116

117+
@handle_internal_errors
116118
def on_response(response: ResponseT, span: LogfireSpan) -> ResponseT:
117119
"""Updates the span based on the type of response."""
118120
if isinstance(response, LegacyAPIResponse): # pragma: no cover
119121
on_response(response.parse(), span) # type: ignore
120122
return cast('ResponseT', response)
121123

122-
if isinstance(response, ChatCompletion):
124+
if isinstance(response, ChatCompletion) and response.choices:
123125
span.set_attribute(
124126
'response_data',
125127
{'message': response.choices[0].message, 'usage': response.usage},
126128
)
127-
elif isinstance(response, Completion):
129+
elif isinstance(response, Completion) and response.choices:
128130
first_choice = response.choices[0]
129131
span.set_attribute(
130132
'response_data',

0 commit comments

Comments
 (0)