Closed
Conversation
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 Agent can help with this pull request. Just |
Deploying with
|
| 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 |
Run ruff format on updated parsing logic and regression test. Co-authored-by: jason <jason@jxnl.co>
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.
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
instructorcan 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_jsonand only falls back to the coercion path uponValidationError.Issue ticket number and link
Linear: 567-268
GitHub: #1616
Checklist before requesting a review
Linear Issue: 567-268