Skip to content

fix: preserve Anthropic thinking block signature for multi-turn conversations#218

Open
wnxd wants to merge 2 commits into
JohnnyZ93:mainfrom
wnxd:fix/anthropic-thinking-signature
Open

fix: preserve Anthropic thinking block signature for multi-turn conversations#218
wnxd wants to merge 2 commits into
JohnnyZ93:mainfrom
wnxd:fix/anthropic-thinking-signature

Conversation

@wnxd
Copy link
Copy Markdown

@wnxd wnxd commented Apr 29, 2026

Fixes #217

Problem

When using Anthropic models with thinking enabled, follow-up requests fail with thinking.signature: Field required because the extension discards signature_delta from streaming responses.

Solution

Uses a static cache keyed by the assistant text content (which VS Code always preserves) to store thinking+signature pairs.

  • During streaming: accumulate assistant text + thinking content + signature
  • On stream end: cache {thinking, signature}[] keyed by assistant text
  • In convertMessages(): look up cached thinking blocks by assistant text
  • If no cache hit: skip thinking block entirely (avoiding 400 error)

Files Changed

  • src/anthropic/anthropicApi.ts (+85 lines)
  • src/commonApi.ts (+13 lines)

Testing

Tested with Claude models using thinking: {type: adaptive} — multi-turn conversations now work correctly.

…rsations

When using Anthropic models with thinking/reasoning enabled, follow-up
requests fail with 'thinking.signature: Field required' because the
extension discards the signature_delta from streaming responses.

VS Code's proposed API does not preserve LanguageModelThinkingPart
(id, metadata, or even the part itself) across conversation turns, so
previous approaches to carry the signature through metadata or ID-based
caches all failed.

This fix uses a static cache keyed by the assistant's text content
(which VS Code always preserves) to store thinking+signature pairs:

- During streaming: accumulate assistant text + thinking content + signature
- On stream end: cache {thinking, signature}[] keyed by assistant text
- In convertMessages(): look up cached thinking blocks by assistant text
- If no cache hit: skip thinking block entirely (avoiding 400 error)

Fixes: multi-turn Anthropic conversations with thinking enabled
Comment thread src/anthropic/anthropicApi.ts
Comment thread src/anthropic/anthropicApi.ts Outdated
Comment thread src/anthropic/anthropicApi.ts Outdated
- Reset _pendingThinkingEntries to undefined after caching to prevent
  redundant writes if the code path is reached twice
- Remove dead code branch: the else-if checking thinkingSignatures can
  never execute since LanguageModelThinkingPart has no signature field
- Remove unused variables: thinkingParts, thinkingSignatures, joinedThinking
Copy link
Copy Markdown
Author

@wnxd wnxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review! All three points have been addressed in commit 5c5e8e1:

1. _pendingThinkingEntries not reset — Fixed. Added this._pendingThinkingEntries = undefined; right after the cache write, and captured entryCount beforehand for the logger.

2. Dead code branch (else if (thinkingSignatures.some(...))) — Removed. Since LanguageModelThinkingPart has no signature field, the thinkingSignatures array was always empty, making this branch unreachable.

3. thinkingSignatures always empty — Removed the variable entirely, along with thinkingParts and joinedThinking which were only consumed by the deleted branch.

@JohnnyZ93
Copy link
Copy Markdown
Owner

There are two issues to consider:

  1. If some API providers do not return the signature, then the thinking part content will be lost during message conversion.
  2. The cache uses _accumulatedAssistantText as the key. If one request does not return text, only thinking and tool_use, will it become invalid?

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.

Bug: Anthropic thinking signature missing in multi-turn conversations

2 participants