Add ensure_valid_tool_response: outbound pre-flight normalizer for circuit breaker prevention#1780
Open
gdeyoung wants to merge 1 commit into
Open
Conversation
Adds ensure_valid_tool_response() to helpers/extract_tools.py as an
outbound pre-flight check that normalizes agent output into a valid
{tool_name, tool_args} envelope before the framework circuit breaker
processes it.
Handles four cases:
1. Already valid tool-call JSON -> pass through
2. Parseable JSON but not a tool call -> wrap in response()
3. Completely non-JSON (plain prose) -> wrap as response().text
4. Empty/null -> return valid empty response()
Threaded into agent.py at process_tools entry point (line 1411).
Verified: 9/9 tests (6 unit + 3 integration) across 3 containers.
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
Adds
ensure_valid_tool_response()tohelpers/extract_tools.py— an outbound pre-flight check that normalizes agent output into a valid{tool_name, tool_args}envelope before the framework circuit breaker processes it. This addresses a gap where models that return plain text instead of structured tool calls (or where the agent itself emits malformed JSON) would trip the "N consecutive unusable model responses" safety breaker.Background
The existing
extract_tools.pyhandles inbound multi-vendor dialect normalization (Anthropic tool_use blocks, DeepSeek singular tool_call, reasoning_content extraction, etc.). But there is a second failure mode: self-inflicted malformation, where the agent emits non-JSON or plain prose instead of a tool-call envelope. The inbound parser cannot fix this because there is nothing to parse.This PR adds the second layer: an outbound pre-flight check that runs before the circuit breaker counts any strikes.
Function Logic
Changes
ensure_valid_tool_response()(~30 lines)process_toolsentry point (line 1411)Testing
Related
Full writeup: https://gist.github.com/gdeyoung/[pending] (multi-vendor writeup with both layers documented)
See also: the existing multi-vendor dispatcher writeup at
documents/a0-multivendor-tool-call-extraction-writeup.md