fix(proxy): stringify tool_result images for OpenAI upstreams

This commit is contained in:
seilk committed 2026-04-30 00:55:45 +09:00
1 parent b33277bf93
commit f6bb31c956
2 files changed
+30 -7

No files matched your search

+12 -1
View File
@@ -219,7 +219,18 @@ function convertToolResultContent(content: unknown, isError: boolean, label: str
} }
if (parsed.type === 'image') { if (parsed.type === 'image') {
throw new Error(`${label}[${index}].type "image" is not supported in tool_result content`); const source =
typeof parsed.source === 'object' && parsed.source !== null
? (parsed.source as Record<string, unknown>)
: undefined;
const description =
source?.type === 'url' && typeof source.url === 'string'
? source.url
: source?.type === 'base64' && typeof source.media_type === 'string'
? `${source.media_type} base64 payload`
: 'unsupported image payload';
parts.push(`[tool_result image omitted: ${description}]`);
continue;
} }
if (typeof parsed.text === 'string') { if (typeof parsed.text === 'string') {
@@ -189,9 +189,8 @@ describe('ProxyRequestTransformer regressions', () => {
).toThrow('must start with tool_result blocks for pending tool_use ids'); ).toThrow('must start with tool_result blocks for pending tool_use ids');
}); });
it('rejects tool_result content that cannot be represented as OpenAI tool text', () => { it('converts tool_result image blocks to text placeholders for OpenAI-compatible tool messages', () => {
expect(() => const result = new ProxyRequestTransformer().transform({
new ProxyRequestTransformer().transform({
messages: [ messages: [
{ {
role: 'assistant', role: 'assistant',
@@ -203,13 +202,26 @@ describe('ProxyRequestTransformer regressions', () => {
{ {
type: 'tool_result', type: 'tool_result',
tool_use_id: 'toolu_1', tool_use_id: 'toolu_1',
content: [{ type: 'image', source: { type: 'url', url: 'https://example.com/error.png' } }], content: [
{ type: 'text', text: 'screenshot captured' },
{ type: 'image', source: { type: 'url', url: 'https://example.com/error.png' } },
{
type: 'image',
source: { type: 'base64', media_type: 'image/png', data: 'ZmFrZQ==' },
}, },
], ],
}, },
], ],
}) },
).toThrow('type "image" is not supported in tool_result content'); ],
});
expect(result.messages[1]).toEqual({
role: 'tool',
tool_call_id: 'toolu_1',
content:
'screenshot captured\n[tool_result image omitted: https://example.com/error.png]\n[tool_result image omitted: image/png base64 payload]',
});
}); });
it('rejects unsupported assistant blocks instead of silently dropping them', () => { it('rejects unsupported assistant blocks instead of silently dropping them', () => {