Skip to content

OpenRouter gemini nested objects#2009

Closed
jxnl wants to merge 5 commits intomainfrom
cursor/567-268-openrouter-gemini-nested-objects-a3d3
Closed

OpenRouter gemini nested objects#2009
jxnl wants to merge 5 commits intomainfrom
cursor/567-268-openrouter-gemini-nested-objects-a3d3

Conversation

@jxnl
Copy link
Collaborator

@jxnl jxnl commented Jan 16, 2026

fix: correctly parse nested JSON strings from OpenRouter+Gemini responses

Describe your changes

This PR resolves an issue where nested objects returned by Gemini models via OpenRouter were serialized as JSON strings within the overall JSON response. This prevented Pydantic from correctly validating and parsing the structured output.

The change introduces a type-aware coercion layer that automatically deserializes these nested JSON strings back into Python objects (dicts or lists) before Pydantic validation. This ensures that instructor can correctly process structured outputs from OpenRouter+Gemini, maintaining compatibility with features like load balancing and fallback.

In strict mode, the system first attempts normal model_validate_json and only falls back to the coercion path upon ValidationError.

Issue ticket number and link

Linear: 567-268
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-268

Open in Cursor Open in Web

cursoragent and others added 3 commits January 16, 2026 17:27
Handle Gemini via OpenRouter returning nested objects as JSON strings by type-aware coercion during parsing. Adds regression test for nested models.

Co-authored-by: jason <jason@jxnl.co>
Tests expect openai_schema() to raise TypeError when decorating non-BaseModel classes.

Co-authored-by: jason <jason@jxnl.co>
In strict mode, try normal validate_json first and only fall back to type-aware nested JSON string coercion on ValidationError.

Co-authored-by: jason <jason@jxnl.co>
@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

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 16, 2026

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 530c9dc Commit Preview URL

Branch Preview URL
Jan 16 2026, 05:45 PM

@github-actions github-actions bot added bug Something isn't working enhancement New feature or request python Pull requests that update python code size:M This PR changes 30-99 lines, ignoring generated files. labels Jan 16, 2026
cursoragent and others added 2 commits January 16, 2026 17:33
Run ruff format on updated parsing logic and regression test.

Co-authored-by: jason <jason@jxnl.co>
@jxnl jxnl closed this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request python Pull requests that update python code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants