Skip to content

🤯 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.

:bug: fix: should not pass `input_image` in message content when has no `image_url` (#12017)

Rylan Cai5fe0ac228e22
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.test.ts b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
index 3a21504ccb4..e0d3a5d05ec 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
@@ -397,6 +397,53 @@ describe('convertOpenAIResponseInputs', () => {
{ content: '杭州天气如何', role: 'user' },
]);
});
+
+ it('should handle openai and claude mixed message', async () => {
+ // See: https://github.com/lobehub/lobehub/pull/12017
+ const messages: OpenAIChatMessage[] = [
+ {
+ content: 'system prompts',
+ role: 'system',
+ },
+ {
+ content: '你是谁',
+ role: 'user',
+ },
+ {
+ content: [
+ {
+ signature: 'E',
+ thinking: 'thoughts',
+ type: 'thinking',
+ },
+ {
+ text: '我是 Claude',
+ type: 'text',
+ },
+ ],
+ role: 'assistant',
+ reasoning: {
+ content: 'The user is asking',
+ duration: 110,
+ // @ts-expect-error: ignore
+ signature: 'E',
+ },
+ },
+ ];
+ const result = await convertOpenAIResponseInputs(messages);
+ expect(result).toEqual([
+ { content: 'system prompts', role: 'developer' },
+ { content: '你是谁', role: 'user' },
+ {
+ summary: [{ text: 'The user is asking', type: 'summary_text' }],
+ type: 'reasoning',
+ },
+ {
+ content: [{ text: '我是 Claude', type: 'output_text' }],
+ role: 'assistant',
+ },
+ ]);
+ });
});
describe('convertImageUrlToFile', () => {
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.ts b/packages/model-runtime/src/core/contextBuilders/openai.ts
index ca361a34ebb..1445882edb7 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.ts
@@ -116,27 +116,37 @@ export const convertOpenAIResponseInputs = async (
// default item
// also need handle image
+
+ const processedContent =
+ typeof message.content === 'string'
+ ? message.content
+ : await Promise.all(
+ (message.content || []).map(async (c) => {
+ if (c.type === 'text') {
+ // if assistant message, set type to output_text
+ // https://platform.openai.com/docs/guides/text
+ if (message.role === 'assistant') {
+ return { ...c, type: 'output_text' };
+ }
+ return { ...c, type: 'input_text' };
+ }
+ const image = await convertMessageContent(c as OpenAI.ChatCompletionContentPart);
+ if (!(image as OpenAI.ChatCompletionContentPartImage).image_url?.url) {
+ return undefined;
+ }
+ return {
+ image_url: (image as OpenAI.ChatCompletionContentPartImage).image_url?.url,
+ type: 'input_image',
+ };
+ }),
+ );
+
const item = {
...message,
content:
- typeof message.content === 'string'
- ? message.content
- : await Promise.all(
- (message.content || []).map(async (c) => {
- if (c.type === 'text') {
- return { ...c, type: 'input_text' };
- }
-
- const image = await convertMessageContent(
- c as OpenAI.ChatCompletionContentPart,
- options,
- );
- return {
- image_url: (image as OpenAI.ChatCompletionContentPartImage).image_url?.url,
- type: 'input_image',
- };
- }),
- ),
+ typeof processedContent === 'string'
+ ? processedContent
+ : processedContent.filter((m) => m !== undefined),
} as OpenAI.Responses.ResponseInputItem;
// remove reasoning field from the message item