Skip to content

Conversation

@jxnl
Copy link
Collaborator

@jxnl jxnl commented Jan 16, 2026

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 instructor from 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 the parse_tools path). 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

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • If it is a core feature, I have added documentation.

Linear Issue: 567-270

Open in Cursor Open in Web

Handle providers (e.g., OpenRouter+Gemini) that return nested objects as JSON strings inside tool-call arguments by coercing those strings back into dict/list before validation.

Also align openai_schema invalid input exception type/message with existing test expectations and add a regression test.

Co-authored-by: jason <[email protected]>
@cursor
Copy link

cursor bot commented Jan 16, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@github-actions github-actions bot added bug Something isn't working documentation Improvements or additions to documentation python Pull requests that update python code size:M This PR changes 30-99 lines, ignoring generated files. type:regression Regression from previous version labels Jan 16, 2026
@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
instructor 1cc3c29 Commit Preview URL

Branch Preview URL
Jan 16 2026, 05:29 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation python Pull requests that update python code size:M This PR changes 30-99 lines, ignoring generated files. type:regression Regression from previous version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants