Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ai-gateway): drop tool messages with missing tool_call_id

Louis Beaumont9371579352e0
diff --git a/packages/ai-gateway/src/providers/openai.ts b/packages/ai-gateway/src/providers/openai.ts
index 15fea9e62..d8970edb2 100644
--- a/packages/ai-gateway/src/providers/openai.ts
+++ b/packages/ai-gateway/src/providers/openai.ts
@@ -227,7 +227,11 @@ export class OpenAIProvider implements AIProvider {
if (call?.id) knownToolCallIds.add(call.id);
}
}
- if (msg.role === 'tool' && msg.tool_call_id && !knownToolCallIds.has(msg.tool_call_id)) {
+ // Drop tool messages with missing OR unknown tool_call_id. OpenAI
+ // 400s either way ("messages with role 'tool' must be a response
+ // to a preceding message with 'tool_calls'"); the previous guard
+ // only caught the second case.
+ if (msg.role === 'tool' && (!msg.tool_call_id || !knownToolCallIds.has(msg.tool_call_id))) {
continue;
}
filtered.push(msg);
diff --git a/packages/ai-gateway/src/providers/vertex-maas.ts b/packages/ai-gateway/src/providers/vertex-maas.ts
index 04c877599..b8f66f1eb 100644
--- a/packages/ai-gateway/src/providers/vertex-maas.ts
+++ b/packages/ai-gateway/src/providers/vertex-maas.ts
@@ -350,7 +350,13 @@ export class VertexMaasProvider implements AIProvider {
};
const isOrphanToolMessage = (msg: Message): boolean => {
if (msg.role === 'tool') {
- return !!msg.tool_call_id && !knownToolCallIds.has(msg.tool_call_id);
+ // Missing OR unknown tool_call_id — Vertex 400s either way with
+ // "No tool calls but found tool output". The previous guard
+ // (`!!msg.tool_call_id && ...`) kept tool-role messages whose
+ // id was simply absent, which the output mapper below then
+ // stripped via conditional spread — Vertex saw a bare role:'tool'
+ // and rejected the whole batch.
+ return !msg.tool_call_id || !knownToolCallIds.has(msg.tool_call_id);
}
if (Array.isArray(msg.content)) {
const hasToolResult = (msg.content as any[]).some((p) => p?.type === 'tool_result');
diff --git a/packages/ai-gateway/src/test/openai-models.unit.test.ts b/packages/ai-gateway/src/test/openai-models.unit.test.ts
index c4f867d66..f9d0d89e6 100644
--- a/packages/ai-gateway/src/test/openai-models.unit.test.ts
+++ b/packages/ai-gateway/src/test/openai-models.unit.test.ts
@@ -212,6 +212,23 @@ describe('OpenAI API accounting and routing', () => {
expect(formatted.map((m: any) => m.role)).toEqual(['user', 'user']);
});
+ // Tool messages with no tool_call_id at all are equally invalid — OpenAI
+ // requires one to bind back to the prior assistant.tool_calls turn.
+ it('drops tool messages with missing tool_call_id', () => {
+ const provider = new OpenAIProvider('sk-test') as any;
+ const formatted = provider.formatMessages([
+ { role: 'user', content: 'list files' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'call_42', type: 'function', function: { name: 'ls', arguments: '{}' } }],
+ },
+ { role: 'tool', content: 'orphan output, no id' },
+ ]);
+ expect(formatted).toHaveLength(2);
+ expect(formatted.map((m: any) => m.role)).toEqual(['user', 'assistant']);
+ });
+
// Sentry SCREENPIPE-AI-PROXY-Y: "400 Unsupported value: 'temperature'
// does not support 0.7 with this model. Only the default (1) is supported."
// New OpenAI models keep being added to the temperature-locked list. The
diff --git a/packages/ai-gateway/src/test/vertex-maas.unit.test.ts b/packages/ai-gateway/src/test/vertex-maas.unit.test.ts
index a961172a6..1972bfb5e 100644
--- a/packages/ai-gateway/src/test/vertex-maas.unit.test.ts
+++ b/packages/ai-gateway/src/test/vertex-maas.unit.test.ts
@@ -213,6 +213,32 @@ describe('VertexMaasProvider.formatMessages', () => {
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({ role: 'user' });
});
+
+ it('drops tool messages with missing tool_call_id (would otherwise reach Vertex bare)', () => {
+ // Output mapper strips tool_call_id when falsy via conditional spread,
+ // so a kept tool message with no id reaches Vertex as { role: 'tool' }
+ // alone and trips 400 "No tool calls but found tool output".
+ const result = provider.formatMessages([
+ { role: 'user', content: 'list files' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'call_42', type: 'function', function: { name: 'ls', arguments: '{}' } }],
+ } as any,
+ { role: 'tool', content: 'orphan output, no id' } as any,
+ ]);
+ expect(result).toHaveLength(2);
+ expect(result.map((m: any) => m.role)).toEqual(['user', 'assistant']);
+ });
+
+ it('drops tool messages with empty-string tool_call_id', () => {
+ const result = provider.formatMessages([
+ { role: 'user', content: 'hi' },
+ { role: 'tool', content: 'empty id', tool_call_id: '' } as any,
+ ]);
+ expect(result).toHaveLength(1);
+ expect(result[0]).toMatchObject({ role: 'user' });
+ });
});
describe('promoteReasoningStream', () => {