-
-
Notifications
You must be signed in to change notification settings - Fork 869
fix(anthropic): ensure tool_result blocks are created for all tool_use responses during reask #1832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…e responses during reask Fixes #1829 When validation fails in ANTHROPIC_TOOLS mode, the reask mechanism was incorrectly skipping tool_result block creation due to an overly restrictive condition that checked: 1. Exception type (ValidationError or InstructorValidationError) 2. Exception title matching the tool name However, when exceptions are wrapped in InstructorError during the reask process, they lose their original type, causing the condition to fail. This resulted in plain text user messages instead of properly formatted tool_result blocks, which violates Anthropic's API requirement that every tool_use must have a corresponding tool_result block. The fix simplifies the logic to always create a tool_result block whenever there's a tool_use in the response, regardless of exception type or title matching. This ensures proper message structure for retries and allows Claude to understand the validation error and correct its response. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Deploying with
|
Status | Name | Latest Commit | Updated (UTC) |
---|---|---|---|
✅ Deployment successful! View logs |
instructor | 8de8b36 | Oct 01 2025, 02:20 PM |
Claude finished @jxnl's task —— View job Code Review: Anthropic Tool Result FixTodo List
🔍 Review SummaryThis PR addresses a critical bug in the Anthropic tools retry mechanism. The fix is well-reasoned and correct, simplifying overly complex conditional logic that was causing 400 errors from Anthropic's API. ✅ Positive AspectsCorrect Problem Identification
Appropriate Solution
Code Quality
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important
Looks good to me! 👍
Reviewed everything up to 8de8b36 in 1 minute and 59 seconds. Click for details.
- Reviewed
17
lines of code in1
files - Skipped
0
files when reviewing. - Skipped posting
2
draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/providers/anthropic/utils.py:159
- Draft comment:
The condition has been simplified to always capture a 'tool_use' by checking only content.type. This resolves the issue with wrapped exceptions, but please add an inline comment noting that the exception-type check was removed because exceptions are wrapped in InstructorError and lose their original type. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 85% The comment is about a real code change in the diff - the removal of exception type checking. The suggestion to add documentation seems reasonable since this is a non-obvious change that affects error handling behavior. However, the comment is asking for documentation rather than pointing out a clear bug or issue. Per the rules, we should not make purely informative comments or ask authors to add documentation unless it's for library code changes. This could be an important change in error handling behavior that deserves documentation. Removing the validation checks could have unintended consequences. While documentation would be nice, our rules explicitly state not to make purely informative comments. The code change appears intentional and works correctly without the additional checks. Delete the comment. While documentation would be helpful, we should not make comments that merely suggest adding documentation unless it's for library code changes.
2. instructor/providers/anthropic/utils.py:159
- Draft comment:
Note that if multiple 'tool_use' messages are present in response.content, only the last one's id will be used when appending the tool_result block. Confirm that this behavior is acceptable with Anthropic’s API, or consider handling multiple tool_use blocks if needed. - Reason this comment was not posted:
Confidence changes required:80%
<= threshold85%
None
Workflow ID: wflow_AEVUqusuWQKgFY6g
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Summary
Fixes #1829 - Resolves 400 error from Anthropic API when validation fails in ANTHROPIC_TOOLS mode
Problem
When using
ANTHROPIC_TOOLS
mode with validation that fails on the first attempt, the retry mechanism was creating malformed message structures. Thereask_anthropic_tools
function had an overly restrictive condition that prevented propertool_result
blocks from being created when exceptions were wrapped inInstructorError
.This caused Anthropic's API to reject requests with:
Root Cause
The condition checking for tool_result creation required:
ValidationError
orInstructorValidationError
However, during the reask process, exceptions are wrapped in
InstructorError
viaInstructorError.from_exception()
, losing their original type. This caused the condition to fail, resulting in plain text user messages instead of properly formattedtool_result
blocks.Solution
Simplified the logic to always create a
tool_result
block whenever there's atool_use
in the response, regardless of exception type or title matching. This ensures:Testing
Generated with Claude Code
Important
Fixes 400 error in Anthropic API by ensuring
tool_result
blocks are created for alltool_use
responses inreask_anthropic_tools()
.tool_result
blocks are created for alltool_use
responses inreask_anthropic_tools()
.tool_result
block whentool_use
is present, regardless of exception type or title.This description was created by
for 8de8b36. You can customize this summary. It will automatically update as commits are pushed.