fix: sanitize malformed thinking blocks before Anthropic serialization - #69
Open
Michael J. Jabbour (michaeljabbour) wants to merge 1 commit into
Conversation
Cross-provider sessions can contain thinking-shaped blocks that the Anthropic API rejects on resume, causing 400 errors and bricked sessions. Added _sanitize_thinking_blocks() in the shared request path (_complete_chat_request, before cache-control application, inherited by fable subclass): strips thinking blocks lacking valid non-empty string signatures, covers both real-world shapes (signature:null from chat-completions; missing signature key from OpenAI Responses producer). - Inserts placeholder when message content empties (API rejects empty arrays) - Preserves valid thinking and redacted_thinking fields - Never raises, logs WARNING on strip, emits provider:thinking_blocks_sanitized event - 19 new tests covering both signature shapes, placeholder insertion, mixed content, no-mutation defensive suite Fixes: microsoft-amplifier/amplifier-support#207 Generated with [Amplifier](https://github.com/microsoft/amplifier) Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.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.
Problem
Cross-provider sessions persist thinking-shaped blocks that the Anthropic API rejects on resume, causing 400 errors and bricked sessions. Two different producers have been observed creating malformed thinking blocks:
reasoning_contentinThinkingBlock(signature: null)thinkingfield and missing/nullsignatureWhen these blocks persist into session history and get replayed to Anthropic on resume, strict validation fails with a 400 that terminates the session (real incidents from both producers confirmed).
Solution
Added
_sanitize_thinking_blocks(messages)staticmethod in the shared request-construction path (_complete_chat_request, before_apply_message_cache_control). This method:thinkingandredacted_thinkingblocks unchangedWARNINGlog +provider:thinking_blocks_sanitizedeventAccuracy note: A hypothetical thinking block missing the
thinkingkey entirely is structurally unreachable—amplifier_core.message_models.ThinkingBlockrequiresthinkingby schema, and real producers always set it. The sanitizer targets only the shapes that actually reach the provider in practice.Testing
Stripped 2 thinking block(s)…logged to stderr. Previously a guaranteed 400.Compatibility
mount(),complete(), or other public contractsFixes microsoft-amplifier/amplifier-support#207