Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ollama-parallel-tool-call-ids.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai-ollama': patch
---

Keep parallel calls to the same tool apart when Ollama sends no tool-call id. The fallback id was `<name>_<Date.now()>`, so two calls to one tool in the same millisecond shared an id and the second call was merged into the first instead of running. The fallback id is now unique per call.
6 changes: 3 additions & 3 deletions packages/ai-ollama/src/adapters/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ export class OllamaTextAdapter<TModel extends string> extends BaseTextAdapter<

const handleToolCall = (toolCall: ToolCall): Array<AdapterYieldChunk> => {
const actualToolCall = toolCall as ToolCall & {
id: string
function: { index: number }
id?: string
function: { index?: number }
}
const toolCallId =
actualToolCall.id || `${actualToolCall.function.name}_${Date.now()}`
actualToolCall.id || generateId(actualToolCall.function.name)
const events: Array<AdapterYieldChunk> = []

// Emit TOOL_CALL_START if not already emitted for this tool call
Expand Down
42 changes: 41 additions & 1 deletion packages/ai-ollama/tests/text-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ beforeEach(() => {

afterEach(() => {
vi.unstubAllEnvs()
vi.restoreAllMocks()
})

describe('OllamaTextAdapter construction', () => {
Expand Down Expand Up @@ -277,7 +278,46 @@ describe('OllamaTextAdapter.chatStream (tool calls)', () => {
const startChunk = chunks.find((c) => c.type === 'TOOL_CALL_START') as
| { toolCallId: string; toolCallName: string }
| undefined
expect(startChunk!.toolCallId).toMatch(/^search_\d+/)
expect(startChunk!.toolCallId).toMatch(/^search-\d+-/)
})

it('keeps two parallel calls to the same tool apart when Ollama omits ids', async () => {
// Same millisecond for both calls: the old `name_Date.now()` id collided here
vi.spyOn(Date, 'now').mockReturnValue(1_700_000_000_000)
chatMock.mockResolvedValueOnce(
asyncIterable([
{
message: {
role: 'assistant',
content: '',
tool_calls: [
{ function: { name: 'search', arguments: { q: 'cats' } } },
{ function: { name: 'search', arguments: { q: 'dogs' } } },
],
},
done: true,
done_reason: 'stop',
},
]),
)

const chunks = await collectStream(
createOllamaChat('llama3.2').chatStream({
threadId: 'thread-1',
logger: testLogger,
model: 'llama3.2',
messages: [{ role: 'user', content: 'find cats and dogs' }],
tools: [searchTool],
}),
)

const startIds = chunks.flatMap((c) =>
c.type === 'TOOL_CALL_START' ? [c.toolCallId] : [],
)
const ends = chunks.flatMap((c) => (c.type === 'TOOL_CALL_END' ? [c] : []))
expect(new Set(startIds).size).toBe(2)
expect(ends.map((e) => e.toolCallId)).toEqual(startIds)
expect(ends.map((e) => e.args)).toEqual(['{"q":"cats"}', '{"q":"dogs"}'])
})

async function toolCallEnd(args: unknown) {
Expand Down
31 changes: 31 additions & 0 deletions testing/e2e/fixtures/tool-calling/parallel-same-tool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"fixtures": [
{
"match": {
"userMessage": "[parallel-same] compare guitars 1 and 2, then 3 and 4",
"sequenceIndex": 0
},
"response": {
"toolCalls": [
{
"name": "compareGuitars",
"arguments": "{\"guitarIds\":[1,2]}"
},
{
"name": "compareGuitars",
"arguments": "{\"guitarIds\":[3,4]}"
}
]
}
},
{
"match": {
"userMessage": "[parallel-same] compare guitars 1 and 2, then 3 and 4",
"sequenceIndex": 1
},
"response": {
"content": "Both comparisons are done. The Stratocaster is the cheapest of the first pair."
}
}
]
}
1 change: 1 addition & 0 deletions testing/e2e/src/lib/feature-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const matrix: Record<Feature, Set<Provider>> = {
'cloudflare',
'neon',
'neon-anthropic',
'ollama',
]),
// Gemini excluded: approval flow timing issues with Gemini's streaming format
// Vertex uses the same Gemini stream, so it is excluded for the same reason.
Expand Down
24 changes: 24 additions & 0 deletions testing/e2e/tests/parallel-tool-calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,29 @@ for (const provider of providersFor('parallel-tool-calls')) {
// Wait for text response after tool execution
await waitForAssistantText(page, 'Stratocaster')
})

// Ollama sends no tool-call ids, so this guards the fallback id
test('keeps parallel calls to the same tool apart', async ({
page,
testId,
aimockPort,
}) => {
await page.goto(
featureUrl(provider, 'parallel-tool-calls', testId, aimockPort),
)

await sendMessage(
page,
'[parallel-same] compare guitars 1 and 2, then 3 and 4',
)
await waitForResponse(page)

const toolNames = (await getToolCalls(page)).map((t) => t.name)
expect(
toolNames.filter((name) => name === 'compareGuitars'),
).toHaveLength(2)

await waitForAssistantText(page, 'Both comparisons are done')
})
})
}
Loading