OpenRouter gemini nested objects #2004
Draft
+125
−7
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.
fix: handle nested JSON string serialization in OpenRouter+Gemini tool calls
Describe your changes
This PR addresses an issue where OpenRouter, when using Gemini, serializes nested objects within tool call arguments as JSON-encoded strings instead of actual objects. This prevents
instructorfrom correctly parsing the tool calls and impacts features like load balancing and fallback.The fix introduces a fallback mechanism in
OpenAISchema.from_response(specifically in theparse_toolspath). If the initial Pydantic validation fails, it attempts to recursively decode any JSON-encoded strings found within the tool call arguments back into Python dictionaries or lists before retrying validation. This ensures that nested structures are correctly parsed.A regression test has been added to simulate the OpenRouter+Gemini payload and confirm the fix.
Issue ticket number and link
Linear: 567-270
GitHub: #1616
Checklist before requesting a review
Linear Issue: 567-270