Skip to content

Fix schema JSON parsing for intermediate tool-call responses#650

Merged
crmne merged 3 commits into
crmne:mainfrom
trouni:fix/schema-tool-call-json-parsing
Mar 4, 2026
Merged

Fix schema JSON parsing for intermediate tool-call responses#650
crmne merged 3 commits into
crmne:mainfrom
trouni:fix/schema-tool-call-json-parsing

Conversation

@trouni

@trouni trouni commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #649

When both with_schema and with_tool are used together, the complete method unconditionally parses any string response.content as 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:

text.text: Input should be a valid string

Root cause

In lib/ruby_llm/chat.rb, the JSON parsing condition:

if @schema && response.content.is_a?(String)

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:

if @schema && response.content.is_a?(String) && !response.tool_call?

Test

Added a regression test that mocks the provider to return an intermediate tool-call response with JSON-like text content, verifying that:

  • The intermediate tool-call message keeps its content as a String
  • The final non-tool-call response has its content parsed into a Hash

trouni and others added 3 commits March 4, 2026 12:14
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 crmne merged commit beec837 into crmne:main Mar 4, 2026
23 of 39 checks passed
@codecov

codecov Bot commented Mar 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.34%. Comparing base (363d3a7) to head (b7eac35).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] with_schema + with_tools: JSON parsing on intermediate tool-call responses causes Anthropic API error

2 participants