fix(providers): tool-use translation correctness (3 audit bugs)#332
Merged
Destynova2 merged 1 commit intomainfrom Apr 28, 2026
Merged
fix(providers): tool-use translation correctness (3 audit bugs)#332Destynova2 merged 1 commit intomainfrom
Destynova2 merged 1 commit intomainfrom
Conversation
Surfaces silent translation failures and preserves client-supplied tool IDs across the canonical->OpenAI / Anthropic upstream round trip. * Bug #1 (tool_use serialization): replace `serde_json::to_string(...).unwrap_or_default()` in `extract_tool_calls` with proper error propagation. Adds `TransformError` enum (`ToolInputSerialization { tool_name, source }`) and a `From<TransformError> for ProviderError` bridge so existing callers continue to surface the failure as `ProviderError::SerializationError`. * Bug #2 (tool ID round trip): introduces `OriginalToolIdMap` populated by `sanitize_tool_use_ids` before the upstream Anthropic call, and applied by `restore_original_tool_ids` on the response so downstream clients (e.g. Claude Code) can match response IDs back to their tool definitions even when the original ID violated Anthropic's `^[a-zA-Z0-9_-]{1,64}$` pattern. Map is scoped to the single provider call (no global state). * Bug #3 (system ordering): `transform_request` now drops residual `role:"system"` entries from `request.messages` after hoisting the canonical `system` field, preventing duplicate system messages when a client sends `[user, system, assistant]` to the Anthropic-style endpoint and the request is routed to OpenAI. Adds unit tests: - `transform_returns_error_when_tool_input_unserializable` - `transform_propagates_tool_input_error_to_provider_error` - `transform_strips_system_from_messages_after_hoisting` - `transform_strips_system_when_no_hoisted_system_field` - `tool_use_id_round_trip_preserves_original` - `restore_is_noop_when_map_is_empty` - `restore_leaves_unmapped_ids_untouched` Streaming round trip for tool IDs is left as a follow-up TODO; the common non-streaming path is fixed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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 three tool-use translation correctness bugs from the audit. All map to silent failure paths in the canonical -> upstream -> client round trip.
extract_tool_callsinsrc/providers/openai/transform.rsreplacedserde_json::to_string(input).unwrap_or_default()with proper error propagation via a newTransformError::ToolInputSerialization { tool_name, source }. AFrom<TransformError> for ProviderErrorbridge keeps the existing callers compatible (surfaces asProviderError::SerializationError).OriginalToolIdMapinsrc/providers/anthropic_sanitize.rs. Populated bysanitize_tool_use_idsbefore the upstream Anthropic call; applied byrestore_original_tool_idson the response so clients (e.g. Claude Code) can map response IDs back to their tool definitions even when the original ID violated Anthropic's^[a-zA-Z0-9_-]{1,64}$. Scoped to the single provider call -- no global state. Streaming SSE rewriting is left as a TODO; the non-streaming path is the common case.transform_requestnow drops any residualrole:\"system\"entries from the canonicalmessagesarray after hoisting the dedicatedsystemfield. Prevents duplicatesystemmessages when a client sends[user, system, assistant]and the request is routed to OpenAI. Includes adebug_assert!guard against future regressions.Tests added
transform_returns_error_when_tool_input_unserializabletransform_propagates_tool_input_error_to_provider_errortransform_strips_system_from_messages_after_hoistingtransform_strips_system_when_no_hoisted_system_fieldtool_use_id_round_trip_preserves_originalrestore_is_noop_when_map_is_emptyrestore_leaves_unmapped_ids_untouchedTest plan
cargo check --testscargo fmt -- --checkon changed filescargo clippy --tests --all-targets -- -D warningscargo nextest run --lib(1113 tests pass)