Skip to content

fix(anthropic): keep signatures per thinking block and keep redacted thinking - #816

Open
ardazeybek wants to merge 1 commit into
huggingface:mainfrom
ardazeybek:fix/anthropic-thinking-signatures
Open

ardazeybek wants to merge 1 commit into
huggingface:mainfrom
ardazeybek:fix/anthropic-thinking-signatures

Conversation

@ardazeybek

Copy link
Copy Markdown

When Anthropic returns more than one thinking block in a turn (interleaved thinking around text or tool use), every signature_delta was appended to a single string. The finished message ended up with one thinking block carrying SIG1SIG2 and the next one with no signature at all, so replaying that history to Anthropic fails signature validation.

redacted_thinking blocks were dropped completely, even though ThinkingContent.redacted exists. Anthropic expects those blocks to be sent back unchanged on the next request, so a conversation that hit one could not continue correctly.

What changed:

  • The parser tracks thinking per content block index, so each signature lands on its own block.
  • A redacted_thinking block becomes ThinkingContent(thinking="", redacted=True) with the encrypted data stored in thinking_signature, and the request builder sends it back as {"type": "redacted_thinking", "data": ...}.
  • ProviderThinkingDeltaEvent got a new_block flag so the stream canonicalizer opens a new thinking block at each provider block boundary instead of merging adjacent ones. Empty deltas (used to mark a redacted block's position) do not emit a ThinkingDeltaEvent.

Checks:

  • Added two tests in tests/test_anthropic_sse.py: per-block signatures, and redacted thinking kept plus replayed in the next request body. Both fail without the change.
  • uv run ruff check . and uv run ruff format --check . are clean; the full test suite passes locally.

I did not test against the live API; the streams in the tests are hand-written from the documented event shapes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant