Fix ResponseTextDeltaEvent validation error for non-OpenAI model providers #1226
+1
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
ChatCmplStreamHandler.handle_stream()
method fails with a Pydantic validation error when creatingResponseTextDeltaEvent
objects during streaming operations with non-OpenAI model providers (e.g., Gemini via Google's OpenAI-compatible API).Error:
Root Cause:
The
ResponseTextDeltaEvent
constructor inchatcmpl_stream_handler.py:193
is missing the requiredlogprobs
parameter. The OpenAI SDK type definition forResponseTextDeltaEvent
defineslogprobs: List[Logprob]
as a required field, but the Agents SDK doesn't provide it when creating these events.Type Inconsistency:
ResponseTextDeltaEvent.logprobs
:List[Logprob]
(required)ResponseOutputText.logprobs
:Optional[List[Logprob]] = None
(optional)This inconsistency causes issues when using model providers that don't return logprobs data.
Debug information
v0.2.3
Python 3.10
1.97.1
2.11.7
Repro steps
The error occurs in
agents/models/chatcmpl_stream_handler.py
at line 193 whereResponseTextDeltaEvent
is constructed without the requiredlogprobs
field.Expected behavior
Streaming should work seamlessly with all OpenAI-compatible model providers, including those that don't provide logprobs data (like Gemini). The
ResponseTextDeltaEvent
should be created with proper logprobs handling that:Solution
The fix uses
getattr()
to gracefully handle the logprobs field, following the existing pattern used elsewhere in the codebase (like line 91 withchunk.usage
):Before:
After:
Impact
This bug affects:
Testing
I've tested the fix with:
Runner.run_streamed()
raw_response_event
andrun_item_stream_event
handling