🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(model-runtime): convert content parts before sending structured output to the Responses API (#19714)
Arvin Xu994912ec0be5
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.test.ts b/packages/model-runtime/src/core/contextBuilders/openai.test.tsindex 76bbf619606..29c0798f50b 100644--- a/packages/model-runtime/src/core/contextBuilders/openai.test.ts+++ b/packages/model-runtime/src/core/contextBuilders/openai.test.ts@@ -878,6 +878,33 @@ describe('convertOpenAIResponseInputs', () => {]);});+ it('should preserve the requested image detail on input_image parts', async () => {+ const messages: OpenAIChatMessage[] = [+ {+ role: 'user',+ content: [+ { type: 'text', text: 'Inspect the circled area' },+ {+ type: 'image_url',+ image_url: { detail: 'high', url: 'data:image/png;base64,test123' },+ },+ ],+ },+ ];++ const result = await convertOpenAIResponseInputs(messages);++ expect(result).toEqual([+ {+ role: 'user',+ content: [+ { type: 'input_text', text: 'Inspect the circled area' },+ { detail: 'high', image_url: 'data:image/png;base64,test123', type: 'input_image' },+ ],+ },+ ]);+ });+it('应该正确转换包含视频的消息', async () => {const messages: OpenAIChatMessage[] = [{diff --git a/packages/model-runtime/src/core/contextBuilders/openai.ts b/packages/model-runtime/src/core/contextBuilders/openai.tsindex 5d4162a669a..54e773db9a3 100644--- a/packages/model-runtime/src/core/contextBuilders/openai.ts+++ b/packages/model-runtime/src/core/contextBuilders/openai.ts@@ -442,8 +442,13 @@ export const convertOpenAIResponseInputs = async (c as OpenAI.ChatCompletionContentPart,options,);- const url = (image as OpenAI.ChatCompletionContentPartImage).image_url?.url;- return url ? { image_url: url, type: 'input_image' as const } : undefined;+ const imageUrl = (image as OpenAI.ChatCompletionContentPartImage).image_url;+ if (!imageUrl?.url) return undefined;+ return {+ image_url: imageUrl.url,+ type: 'input_image' as const,+ ...(imageUrl.detail && { detail: imageUrl.detail }),+ };}),)).filter((c) => !!c);@@ -515,12 +520,16 @@ export const convertOpenAIResponseInputs = async (c as OpenAI.ChatCompletionContentPart,options,);- if (!(image as OpenAI.ChatCompletionContentPartImage).image_url?.url) {+ const imageUrl = (image as OpenAI.ChatCompletionContentPartImage).image_url;+ if (!imageUrl?.url) {return undefined;}+ // Forward the requested detail level so callers that need full resolution+ // (e.g. inspecting small annotations on screenshots) are not downgraded to auto.return {- image_url: (image as OpenAI.ChatCompletionContentPartImage).image_url?.url,+ image_url: imageUrl.url,type: 'input_image',+ ...(imageUrl.detail && { detail: imageUrl.detail }),};}),);diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/generateObject.test.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/generateObject.test.tsindex ec321a3474b..e3419237da3 100644--- a/packages/model-runtime/src/core/openaiCompatibleFactory/generateObject.test.ts+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/generateObject.test.ts@@ -248,3 +248,63 @@ describe('generateObject tool-calling fallback', () => {expect(getCreateMock(instance)).toHaveBeenCalledTimes(1);});});++describe('generateObject via Responses API', () => {+ afterEach(() => {+ vi.clearAllMocks();+ });++ const createInstance = () => new TestRuntime({ apiKey: 'test' });++ const multimodalPayload = {+ ...generateObjectPayload,+ messages: [+ { content: 'You distil standards.', role: 'system' as const },+ {+ content: [+ { text: 'What is wrong in this frame?', type: 'text' as const },+ {+ image_url: { detail: 'high' as const, url: 'data:image/png;base64,AAAA' },+ type: 'image_url' as const,+ },+ ],+ role: 'user' as const,+ },+ ],+ responseApi: true,+ };++ it('should convert content parts to Responses input parts', async () => {+ const instance = createInstance();+ const createMock = vi+ .spyOn((instance as any).client.responses, 'create')+ .mockResolvedValue({ output_text: '{"summary":"s","title":"t"}' } as any);++ const result = await instance.generateObject(multimodalPayload as any);++ const requestPayload = createMock.mock.calls[0][0] as any;+ expect(requestPayload.input).toEqual([+ { content: 'You distil standards.', role: 'developer' },+ {+ content: [+ { text: 'What is wrong in this frame?', type: 'input_text' },+ { detail: 'high', image_url: 'data:image/png;base64,AAAA', type: 'input_image' },+ ],+ role: 'user',+ },+ ]);+ expect(result).toEqual({ summary: 's', title: 't' });+ });++ it('should leave string content untouched', async () => {+ const instance = createInstance();+ const createMock = vi+ .spyOn((instance as any).client.responses, 'create')+ .mockResolvedValue({ output_text: '{"summary":"s","title":"t"}' } as any);++ await instance.generateObject({ ...generateObjectPayload, responseApi: true } as any);++ const requestPayload = createMock.mock.calls[0][0] as any;+ expect(requestPayload.input).toEqual([{ content: 'Generate a handoff', role: 'user' }]);+ });+});diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.tsindex db8d538c9d0..f7464ad3b36 100644--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts@@ -1188,9 +1188,19 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = anif (shouldUseResponses) {log('calling responses.create for structured output');+ // Chat Completions content parts are not valid Responses input: a `text` part must+ // become `input_text` and an `image_url` part `input_image`, or the API rejects the+ // whole request. String content survives untouched, which is why text-only callers+ // never hit this.+ const input = await convertOpenAIResponseInputs(messages as any, {+ forceImageBase64: chatCompletion?.forceImageBase64,+ forceVideoBase64: chatCompletion?.forceVideoBase64,+ provider: this.id,+ strictToolPairing: true,+ });const preparedRequest = this.prepareResponsesRequest({- input: messages,+ input,model,...getGenerateObjectResponsesReasoningParams(payload),...this.resolvePromptCacheKeyParams(model, options?.user),