-
Notifications
You must be signed in to change notification settings - Fork 79
fix: Cross-provider tool-call ID + thinking-block compatibility #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danny-avila
wants to merge
15
commits into
dev
Choose a base branch
from
claude/elated-vaughan-4e24b9
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,504
−62
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e9329c0
🪪 fix: Normalize Tool-Call IDs For Anthropic Compatibility
danny-avila 02da1c7
🧠 fix: Flatten Anthropic Thinking Blocks For OpenAI Targets
danny-avila e070e41
🪪 fix: Append Hash Suffix To Disambiguate Normalized Tool-Call IDs
danny-avila d9d4898
test: Add live collision case for tool-call ID disambiguation
danny-avila cf431ad
test: Bump OpenAI test model to gpt-5.4-mini
danny-avila 79ac5cc
🧠 fix: Avoid Empty Assistant Content Arrays For OpenAI Targets
danny-avila 1b78e72
🧹 refactor: Address Audit Findings In Cross-Provider Helpers
danny-avila 1ae50c0
🧠 fix: Apply Thinking-Block Flatten On Default OpenAI Path
danny-avila 9df3d83
🧠 fix: Cover All OpenAI-Shaped Wrappers For Cross-Provider Thinking
danny-avila de2c0c0
🧹 refactor: Avoid Parameter Reassignment In ChatDeepSeek._generate
danny-avila 2a1adcc
🧠 fix: Preserve Pre-Flattened Thinking Text On Tool-Call Turns
danny-avila 3ffe05c
🧹 refactor: Single-Pass flatMap In flattenAnthropicThinkingForOpenAI
danny-avila 036fb77
🪪 fix: Detect Claude Targets Case-Insensitively
danny-avila 4da5365
🪪 fix: Add claudeBackend Override For Aliased Claude Deployments
danny-avila 65ba110
🪪 fix: Extend claudeBackend Override To DeepSeek And xAI Wrappers
danny-avila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages'; | ||
| import { | ||
| _convertLangChainToolCallToAnthropic, | ||
| _convertMessagesToAnthropicPayload, | ||
| normalizeAnthropicToolCallId, | ||
| } from './message_inputs'; | ||
|
|
||
| describe('normalizeAnthropicToolCallId', () => { | ||
| it('returns valid IDs unchanged', () => { | ||
| expect(normalizeAnthropicToolCallId('toolu_01ABcdEFgh')).toBe( | ||
| 'toolu_01ABcdEFgh' | ||
| ); | ||
| expect(normalizeAnthropicToolCallId('call_abc123XYZ')).toBe( | ||
| 'call_abc123XYZ' | ||
| ); | ||
| expect(normalizeAnthropicToolCallId('a-b_c-d')).toBe('a-b_c-d'); | ||
| }); | ||
|
|
||
| it('sanitizes invalid characters and appends a hash suffix', () => { | ||
| const out = normalizeAnthropicToolCallId( | ||
| 'fc_67abc1234def567|call_abc123def456ghi789jkl0mnopqrs' | ||
| ); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(out)).toBe(true); | ||
| expect(out.length).toBeLessThanOrEqual(64); | ||
| expect( | ||
| out.startsWith('fc_67abc1234def567_call_abc123def456ghi789jkl0mn') | ||
| ).toBe(true); | ||
| // Suffix is `_<10-hex-char hash>` | ||
| expect(out).toMatch(/_[0-9a-f]{10}$/); | ||
| }); | ||
|
|
||
| it('produces compliant output for IDs of any length', () => { | ||
| const long = 'fc_' + 'a'.repeat(80); | ||
| const out = normalizeAnthropicToolCallId(long); | ||
| expect(out).toHaveLength(64); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(out)).toBe(true); | ||
| }); | ||
|
|
||
| it('produces uniquely distinguishable outputs for IDs that share a 64-char prefix', () => { | ||
| const sharedPrefix = 'fc_' + 'a'.repeat(80); | ||
| const idA = sharedPrefix + '|call_unique_A'; | ||
| const idB = sharedPrefix + '|call_unique_B'; | ||
|
|
||
| const outA = normalizeAnthropicToolCallId(idA); | ||
| const outB = normalizeAnthropicToolCallId(idB); | ||
|
|
||
| expect(outA).not.toBe(outB); | ||
| expect(outA).toHaveLength(64); | ||
| expect(outB).toHaveLength(64); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(outA)).toBe(true); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(outB)).toBe(true); | ||
| }); | ||
|
|
||
| it('disambiguates short IDs that sanitize to the same value', () => { | ||
| expect(normalizeAnthropicToolCallId('a|b')).not.toBe( | ||
| normalizeAnthropicToolCallId('a.b') | ||
| ); | ||
| }); | ||
|
|
||
| it('handles combined length and character violations', () => { | ||
| const id = 'fc_' + 'x|'.repeat(100); | ||
| const out = normalizeAnthropicToolCallId(id); | ||
| expect(out).toHaveLength(64); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(out)).toBe(true); | ||
| }); | ||
|
|
||
| it('is deterministic — same input always yields same output', () => { | ||
| const id = 'fc_a|b|c'; | ||
| expect(normalizeAnthropicToolCallId(id)).toBe( | ||
| normalizeAnthropicToolCallId(id) | ||
| ); | ||
| }); | ||
|
|
||
| it('passes through undefined for the optional overload', () => { | ||
| expect(normalizeAnthropicToolCallId(undefined)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_convertMessagesToAnthropicPayload — cross-provider ID normalization', () => { | ||
| it('normalizes Responses-style IDs on tool_use AND matching tool_result', () => { | ||
| const responsesId = 'fc_67abc1234def567|call_abc123def456ghi789jkl0mnopqrs'; | ||
|
|
||
| const payload = _convertMessagesToAnthropicPayload([ | ||
| new HumanMessage('weather?'), | ||
| new AIMessage({ | ||
| content: '', | ||
| tool_calls: [ | ||
| { | ||
| id: responsesId, | ||
| name: 'get_weather', | ||
| args: { location: 'Tokyo' }, | ||
| type: 'tool_call', | ||
| }, | ||
| ], | ||
| }), | ||
| new ToolMessage({ | ||
| tool_call_id: responsesId, | ||
| content: '{"temp": 21}', | ||
| }), | ||
| ]); | ||
|
|
||
| const assistantMsg = payload.messages.find((m) => m.role === 'assistant')!; | ||
| const userToolResultMsg = payload.messages.find( | ||
| (m) => | ||
| m.role === 'user' && | ||
| Array.isArray(m.content) && | ||
| (m.content as Array<{ type: string }>)[0]?.type === 'tool_result' | ||
| )!; | ||
|
|
||
| const toolUseBlock = ( | ||
| assistantMsg.content as Array<{ type: string; id?: string }> | ||
| ).find((b) => b.type === 'tool_use')!; | ||
| const toolResultBlock = ( | ||
| userToolResultMsg.content as Array<{ | ||
| type: string; | ||
| tool_use_id?: string; | ||
| }> | ||
| ).find((b) => b.type === 'tool_result')!; | ||
|
|
||
| const expected = normalizeAnthropicToolCallId(responsesId); | ||
| expect(toolUseBlock.id).toBe(expected); | ||
| expect(toolResultBlock.tool_use_id).toBe(expected); | ||
| expect(toolUseBlock.id).toBe(toolResultBlock.tool_use_id); | ||
| expect(/^[a-zA-Z0-9_-]+$/.test(toolUseBlock.id!)).toBe(true); | ||
| expect(toolUseBlock.id!.length).toBeLessThanOrEqual(64); | ||
| }); | ||
|
|
||
| it('passes through Anthropic-native IDs unchanged', () => { | ||
| const nativeId = 'toolu_01ABcdEFgh23ijKL'; | ||
|
|
||
| const payload = _convertMessagesToAnthropicPayload([ | ||
| new HumanMessage('hi'), | ||
| new AIMessage({ | ||
| content: '', | ||
| tool_calls: [ | ||
| { | ||
| id: nativeId, | ||
| name: 'noop', | ||
| args: {}, | ||
| type: 'tool_call', | ||
| }, | ||
| ], | ||
| }), | ||
| new ToolMessage({ | ||
| tool_call_id: nativeId, | ||
| content: 'ok', | ||
| }), | ||
| ]); | ||
|
|
||
| const assistantMsg = payload.messages.find((m) => m.role === 'assistant')!; | ||
| const toolUseBlock = ( | ||
| assistantMsg.content as Array<{ type: string; id?: string }> | ||
| ).find((b) => b.type === 'tool_use')!; | ||
|
|
||
| expect(toolUseBlock.id).toBe(nativeId); | ||
| }); | ||
|
|
||
| it('does not normalize server tool IDs (srvtoolu_ prefix)', () => { | ||
| const serverId = 'srvtoolu_01abcXYZ'; | ||
|
|
||
| const block = _convertLangChainToolCallToAnthropic({ | ||
| id: serverId, | ||
| name: 'web_search', | ||
| args: { query: 'x' }, | ||
| type: 'tool_call', | ||
| }); | ||
|
|
||
| expect(block.type).toBe('server_tool_use'); | ||
| expect(block.id).toBe(serverId); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filtering out
thinking/redacted_thinkingblocks can leavecontentas[]for assistant messages that have notool_calls(for example, a message containing onlyredacted_thinkingor only emptythinking). This value is then forwarded unchanged, but Chat Completions requires assistant content arrays to contain at least one part (textor a singlerefusal), so this can still trigger a 400 on the OpenAI path. You already guard this case in the tool-calls branch by converting empty arrays to''; the same normalization is needed for non-tool-call assistant messages.Useful? React with 👍 / 👎.