fix(openai): recover from Gemini-style concatenated JSON tool call arguments in streaming#7549
Open
r266-tech wants to merge 2 commits intomicrosoft:mainfrom
Open
fix(openai): recover from Gemini-style concatenated JSON tool call arguments in streaming#7549r266-tech wants to merge 2 commits intomicrosoft:mainfrom
r266-tech wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…guments
When using Gemini (or other providers that emit a complete JSON object per
streaming chunk rather than incremental deltas), simple string concatenation
produces invalid JSON such as `{}{{"stock":"MSFT"}}`.
Detect and recover by keeping only the last valid complete JSON object in the
accumulated arguments string, using a backwards bracket-depth scan.
Fixes microsoft#6843
…call arguments Adds test_tool_calling_with_stream_gemini_style_arguments which simulates Gemini's behavior of sending a complete empty JSON object in the first chunk followed by the real arguments in a second chunk. Related to fix for microsoft#6843
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #6843 —
openai.BadRequestErrorwhen using Gemini models withmodel_client_stream=Trueand multiple tool calls.Root cause
Some providers (notably Gemini) emit a complete JSON object per streaming chunk rather than incremental argument deltas:
delta.function.arguments{}(empty placeholder){"stock": "MSFT"}(actual arguments)Simple string concatenation then produces
{}{"stock":"MSFT"}— invalid JSON — which fails when the agent tries to call the tool.Fix
After the streaming loop finishes, validate each accumulated
FunctionCall.argumentswithjson.loads. If it fails, walk backwards through the string to find the last valid complete JSON object (bracket-depth scan) and use that as the canonical arguments value.This is a surgical, provider-neutral recovery step that does not change the normal (non-Gemini) streaming path.
Changes
python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py: add recovery step increate_streambefore buildingCreateResultpython/packages/autogen-ext/tests/models/test_openai_model_client.py: addtest_tool_calling_with_stream_gemini_style_argumentsregression testTest
New test simulates Gemini's two-chunk pattern (
{}then{"input": "task"}) and asserts that the recovered arguments are{"input": "task"}(the last valid complete JSON object).