fix(anthropic): strip FORMAT_TOOL from re-encoded history to fix resume() HTTP 400 - #2863
Open
devteamaegis wants to merge 3 commits into
Open
Conversation
willbakst
reviewed
May 29, 2026
| # the next user message — Anthropic rejects the request with 400: | ||
| # "tool_use ids were found without tool_result blocks immediately after" | ||
| # See https://github.com/Mirascope/mirascope/issues/2503 | ||
| if part.name.startswith(FORMAT_TOOL_NAME): |
Collaborator
There was a problem hiding this comment.
Doesn't this mean the content of this final tool call isn't included at all? Shouldn't we be re-encoding it as text in this case?
Per review (@willbakst): skipping the format-tool call entirely lost the assistant's actual structured response. Now re-encode its arguments (the JSON the model produced) as a text block — content preserved, and still no orphaned tool_use that would 400 on resume. Test asserts both text parts.
Author
|
Good catch @willbakst — you're right, |
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.
Problem
response.resume()fails with HTTP 400 when using Anthropic with structured outputs (format=SomeModel):Fixes #2503
Root cause
Anthropic structured-output mode injects a private
FORMAT_TOOL_NAMEtool. The model response contains atool_useblock for that tool. When the assistant message appears in the history of aresume()call:_encode_messagedetects the format tool (raw_message_has_format_tool→True) and falls through to re-encode from content parts instead of using the raw message verbatim.encode_contentencodes everytool_callpart as aToolUseBlockParam— including theFORMAT_TOOL_NAMEcall.tool_resultin the following user message (there never is — the format tool is a Mirascope detail that is never actually invoked).Fix
In
encode_content, skip anytool_callpart whose name starts withFORMAT_TOOL_NAME:This preserves all real tool_use blocks while stripping the implementation-detail format tool from re-encoded history.
Tests
Added to
python/tests/llm/providers/anthropic/test_anthropic_encoding.py:test_encode_content_skips_format_tool_calltest_encode_content_keeps_real_tool_calls