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): preserve assistant tool_calls when content is a string

Louis Beaumont85b0f37c6e7e
diff --git a/packages/ai-gateway/src/providers/vertex-maas.ts b/packages/ai-gateway/src/providers/vertex-maas.ts
index b8f66f1eb..f5cd0711d 100644
--- a/packages/ai-gateway/src/providers/vertex-maas.ts
+++ b/packages/ai-gateway/src/providers/vertex-maas.ts
@@ -382,12 +382,24 @@ export class VertexMaasProvider implements AIProvider {
}
private formatMessageContent(msg: Message): { content: any; tool_calls?: any[] } {
+ // Preserve top-level OpenAI-style tool_calls regardless of content shape.
+ // The common assistant payload is { content: '', tool_calls: [...] }
+ // (string content, not array) — dropping tool_calls here breaks the
+ // next tool-response message because Vertex sees the assistant with
+ // no tool_calls and rejects the batch with "No tool calls but found
+ // tool output".
+ const topLevelToolCalls: any[] = [...((msg as any).tool_calls ?? [])];
+
if (!Array.isArray(msg.content)) {
- return { content: nonEmptyText(msg.content) ?? '' };
+ const out: { content: any; tool_calls?: any[] } = {
+ content: nonEmptyText(msg.content) ?? (topLevelToolCalls.length > 0 ? null : ''),
+ };
+ if (topLevelToolCalls.length > 0) out.tool_calls = topLevelToolCalls;
+ return out;
}
const content: any[] = [];
- const toolCalls: any[] = [...((msg as any).tool_calls ?? [])];
+ const toolCalls: any[] = topLevelToolCalls;
for (const part of msg.content as any[]) {
const type = part?.type;
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 1972bfb5e..c6385dec1 100644
--- a/packages/ai-gateway/src/test/vertex-maas.unit.test.ts
+++ b/packages/ai-gateway/src/test/vertex-maas.unit.test.ts
@@ -239,6 +239,28 @@ describe('VertexMaasProvider.formatMessages', () => {
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({ role: 'user' });
});
+
+ // Without this preservation, the string-content branch of
+ // formatMessageContent silently drops the OpenAI-style tool_calls field,
+ // and Vertex 400s on the next tool-role message because the assistant
+ // payload it sees has no tool_calls to bind to.
+ it('preserves OpenAI-style tool_calls on assistant with string content', () => {
+ const result = provider.formatMessages([
+ { role: 'user', content: 'what files?' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'call_42', type: 'function', function: { name: 'ls', arguments: '{}' } }],
+ } as any,
+ { role: 'tool', content: 'a.txt b.txt', tool_call_id: 'call_42' } as any,
+ ]);
+ expect(result).toHaveLength(3);
+ expect(result[1]).toMatchObject({
+ role: 'assistant',
+ tool_calls: [{ id: 'call_42' }],
+ });
+ expect(result[2]).toMatchObject({ role: 'tool', tool_call_id: 'call_42' });
+ });
});
describe('promoteReasoningStream', () => {