Skip to content

Commit a8c8b03

Browse files
committed
Refine defensive checks based on code review feedback
- Remove StreamingChoices from type annotation since this is non-streaming - Remove redundant type assertion as LiteLLM validates types - Simplify tracing output with ternary expression
1 parent a8e2c78 commit a8c8b03

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/agents/extensions/models/litellm_model.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ async def get_response(
111111
)
112112

113113
message: litellm.types.utils.Message | None = None
114-
first_choice: (
115-
litellm.types.utils.Choices | litellm.types.utils.StreamingChoices | None
116-
) = None
114+
first_choice: litellm.types.utils.Choices | None = None
117115
if response.choices and len(response.choices) > 0:
118-
first_choice = response.choices[0]
119-
assert isinstance(first_choice, litellm.types.utils.Choices)
120-
message = first_choice.message
116+
choice = response.choices[0]
117+
if isinstance(choice, litellm.types.utils.Choices):
118+
first_choice = choice
119+
message = first_choice.message
121120

122121
if _debug.DONT_LOG_MODEL_DATA:
123122
logger.debug("Received model response")
@@ -160,8 +159,10 @@ async def get_response(
160159
usage = Usage()
161160
logger.warning("No usage information returned from Litellm")
162161

163-
if tracing.include_data() and message is not None:
164-
span_generation.span_data.output = [message.model_dump()]
162+
if tracing.include_data():
163+
span_generation.span_data.output = (
164+
[message.model_dump()] if message is not None else []
165+
)
165166
span_generation.span_data.usage = {
166167
"input_tokens": usage.input_tokens,
167168
"output_tokens": usage.output_tokens,

0 commit comments

Comments
 (0)