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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(ai-gateway): backfill missing ids on Vertex MaaS tool calls (#4168)
Louis Beaumont50b2d1ac91de
diff --git a/packages/ai-gateway/src/providers/vertex-maas.ts b/packages/ai-gateway/src/providers/vertex-maas.tsindex f5cd0711d..4e4791c7d 100644--- a/packages/ai-gateway/src/providers/vertex-maas.ts+++ b/packages/ai-gateway/src/providers/vertex-maas.ts@@ -16,7 +16,7 @@*/import { AIProvider } from './base';-import { Message, RequestBody, ResponseFormat } from '../types';+import { Message, RequestBody, ResponseFormat, ToolCall } from '../types';import { VertexAIProvider } from './vertex';const MAX_RETRIES = 3;@@ -221,6 +221,46 @@ export function resolveVertexMaasModel(model: string): { vertexId: string; regioreturn null;}+/**+ * Backfill ids onto assistant `tool_calls` (and the matching tool result) that+ * arrive without one. Vertex MaaS rejects the entire request with+ * `400 INVALID_ARGUMENT "Expected the 'id' of a(n) 'assistant' 'tool_calls'+ * array element to be populated"` when any assistant tool call has an empty or+ * absent id — which happens after some clients reconstruct chat history+ * (SCREENPIPE-AI-PROXY-C).+ *+ * We synthesize a stable id for each such call and hand the same id to the next+ * tool result that is itself missing a `tool_call_id`, pairing them positionally+ * (OpenAI emits tool results in call order). Running before the orphan filter in+ * `formatMessages` means the synthesized ids are visible to it, so a repaired+ * call/result pair survives instead of being dropped or 400ing. Well-formed+ * messages (every tool call already has an id) pass through untouched.+ */+export function backfillToolCallIds(messages: Message[]): Message[] {+ let counter = 0;+ const pendingSynthIds: string[] = [];+ return messages.map((msg) => {+ if (msg.role === 'assistant' && Array.isArray((msg as any).tool_calls)) {+ const calls = (msg as any).tool_calls as ToolCall[];+ // Untouched unless at least one call is missing its id.+ if (!calls.some((c) => !c?.id)) return msg;+ const fixed = calls.map((c) => {+ if (c && !c.id) {+ const id = `call_auto_${counter++}`;+ pendingSynthIds.push(id);+ return { ...c, id };+ }+ return c;+ });+ return { ...msg, tool_calls: fixed };+ }+ if (msg.role === 'tool' && !msg.tool_call_id && pendingSynthIds.length > 0) {+ return { ...msg, tool_call_id: pendingSynthIds.shift()! };+ }+ return msg;+ });+}+export class VertexMaasProvider implements AIProvider {supportsTools = true;supportsVision = true;@@ -330,6 +370,12 @@ export class VertexMaasProvider implements AIProvider {}formatMessages(messages: Message[]): any[] {+ // Repair assistant tool_calls (and their matching tool result) that lack+ // an id before anything else, so Vertex doesn't 400 with "Expected the+ // 'id' of a(n) 'assistant' 'tool_calls' array element to be populated".+ // Runs first so the orphan filter below sees the synthesized ids.+ messages = backfillToolCallIds(messages);+// Drop orphan tool messages (tool_call_id with no matching assistant// tool_calls earlier in the array, or a content `tool_result` part on// a user/tool message whose id was never emitted). Vertex MaaS rejectsdiff --git a/packages/ai-gateway/src/test/vertex-maas.unit.test.ts b/packages/ai-gateway/src/test/vertex-maas.unit.test.tsindex c6385dec1..f762bbf6e 100644--- a/packages/ai-gateway/src/test/vertex-maas.unit.test.ts+++ b/packages/ai-gateway/src/test/vertex-maas.unit.test.ts@@ -4,6 +4,7 @@import { describe, it, expect } from 'bun:test';import {+ backfillToolCallIds,isVertexMaasModel,parseVertexMaasJsonResponse,promoteReasoningStream,@@ -261,6 +262,72 @@ describe('VertexMaasProvider.formatMessages', () => {});expect(result[2]).toMatchObject({ role: 'tool', tool_call_id: 'call_42' });});++ // SCREENPIPE-AI-PROXY-C: a tool-call turn arriving without an id used to be+ // forwarded verbatim, so Vertex 400'd with "Expected the 'id' of a(n)+ // 'assistant' 'tool_calls' array element to be populated" and the whole+ // request failed. formatMessages must hand it a populated id and keep the+ // matching tool result paired to it.+ it('populates id-less assistant tool_calls so Vertex does not 400', () => {+ const result = provider.formatMessages([+ { role: 'user', content: 'list files' },+ {+ role: 'assistant',+ content: '',+ tool_calls: [{ type: 'function', function: { name: 'ls', arguments: '{}' } }],+ } as any,+ { role: 'tool', content: 'a.txt' } as any,+ ]);+ expect(result).toHaveLength(3);+ const id = result[1].tool_calls[0].id;+ expect(id).toBeTruthy();+ expect(result[2]).toMatchObject({ role: 'tool', tool_call_id: id });+ });+});++describe('backfillToolCallIds', () => {+ it('synthesizes an id for an id-less assistant tool_call and pairs its result', () => {+ const out = backfillToolCallIds([+ { role: 'user', content: 'list files' },+ { role: 'assistant', content: '', tool_calls: [{ type: 'function', function: { name: 'ls', arguments: '{}' } }] },+ { role: 'tool', content: 'a.txt' },+ ] as any);+ const id = (out[1] as any).tool_calls[0].id;+ expect(id).toBeTruthy();+ expect((out[2] as any).tool_call_id).toBe(id);+ });++ it('leaves well-formed messages untouched (same object references)', () => {+ const input = [+ { role: 'assistant', content: '', tool_calls: [{ id: 'call_42', type: 'function', function: { name: 'ls', arguments: '{}' } }] },+ { role: 'tool', content: 'x', tool_call_id: 'call_42' },+ ] as any;+ const out = backfillToolCallIds(input);+ expect(out[0]).toBe(input[0]);+ expect(out[1]).toBe(input[1]);+ });++ it('pairs multiple id-less calls to their results in order', () => {+ const out = backfillToolCallIds([+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ { type: 'function', function: { name: 'a', arguments: '{}' } },+ { type: 'function', function: { name: 'b', arguments: '{}' } },+ ],+ },+ { role: 'tool', content: 'result a' },+ { role: 'tool', content: 'result b' },+ ] as any);+ const id0 = (out[0] as any).tool_calls[0].id;+ const id1 = (out[0] as any).tool_calls[1].id;+ expect(id0).toBeTruthy();+ expect(id1).toBeTruthy();+ expect(id0).not.toBe(id1);+ expect((out[1] as any).tool_call_id).toBe(id0);+ expect((out[2] as any).tool_call_id).toBe(id1);+ });});describe('promoteReasoningStream', () => {