Fix schema JSON parsing for intermediate tool-call responses#650
Merged
Conversation
When both `with_schema` and `with_tool` are used, the `complete` method
parses any string content as JSON whenever a schema is set — including
intermediate responses that also contain tool calls. On the next API
call the parsed Hash is serialized as `{ type: "text", text: <Hash> }`,
which providers like Anthropic reject with:
"text.text: Input should be a valid string"
Add `&& !response.tool_call?` guard so JSON parsing is only applied to
the final non-tool-call response.
Fixes crmne#649
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
crmne
approved these changes
Mar 4, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #650 +/- ##
==========================================
- Coverage 81.36% 81.34% -0.02%
==========================================
Files 116 116
Lines 5425 5425
Branches 1426 1426
==========================================
- Hits 4414 4413 -1
- Misses 1011 1012 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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 #649
When both
with_schemaandwith_toolare used together, thecompletemethod unconditionally parses any stringresponse.contentas JSON whenever a schema is set — including intermediate responses that also contain tool calls.On the next API call, the parsed Hash gets serialized as
{ type: "text", text: <Hash> }, which providers like Anthropic reject with:Root cause
In
lib/ruby_llm/chat.rb, the JSON parsing condition:does not check whether the response is an intermediate tool-call response, so it eagerly parses content that should remain as a plain string.
Fix
Add a
!response.tool_call?guard so JSON parsing is only applied to the final (non-tool-call) response:Test
Added a regression test that mocks the provider to return an intermediate tool-call response with JSON-like text content, verifying that:
StringHash