diff --git a/packages/ai/src/protocols/openai-chat.ts b/packages/ai/src/protocols/openai-chat.ts index 0649d28fba2..545adb3f7cf 100644 --- a/packages/ai/src/protocols/openai-chat.ts +++ b/packages/ai/src/protocols/openai-chat.ts @@ -323,7 +323,11 @@ const lowerMedia = Effect.fn("OpenAIChat.lowerMedia")(function* (part: MediaPart const media = ProviderShared.normalizeMedia(part) if (!media.mime.startsWith("image/")) return yield* ProviderShared.invalidRequest(`OpenAI Chat does not support media type ${part.mediaType}`) - return { type: "image_url" as const, image_url: { url: media.dataUrl } } + const url = + typeof part.data === "string" && (part.data.startsWith("https://") || part.data.startsWith("http://")) + ? part.data + : media.dataUrl + return { type: "image_url" as const, image_url: { url } } }) const openAICompatibleReasoningContent = (native: unknown) => diff --git a/packages/ai/test/provider/openai-chat.test.ts b/packages/ai/test/provider/openai-chat.test.ts index 376c6a7b87e..d45613f5990 100644 --- a/packages/ai/test/provider/openai-chat.test.ts +++ b/packages/ai/test/provider/openai-chat.test.ts @@ -698,6 +698,54 @@ describe("OpenAI Chat route", () => { }), ) + it.effect("preserves HTTP and HTTPS image URLs in user content", () => + Effect.gen(function* () { + const urls = ["https://example.com/image.png?size=64#preview", "http://example.com/image.jpg"] + const prepared = yield* compileRequest( + LLM.request({ + model, + prompt: urls.map((data) => ({ type: "media" as const, mediaType: "image/png", data })), + }), + ) + expect(prepared.body.messages).toEqual([ + { role: "user", content: urls.map((url) => ({ type: "image_url", image_url: { url } })) }, + ]) + }), + ) + + it.effect("preserves remote image URLs from tool results", () => + Effect.gen(function* () { + const url = "https://example.com/tool-image.png?version=2" + const prepared = yield* compileRequest( + LLM.request({ + model, + messages: [ + Message.user("Describe the image."), + Message.assistant([ToolCallPart.make({ id: "call_image", name: "read_image", input: {} })]), + Message.tool({ + id: "call_image", + name: "read_image", + resultType: "content", + result: [ + { type: "text", text: "Image attached." }, + { type: "file", mime: "image/png", uri: url }, + ], + }), + ], + }), + ) + expect(prepared.body.messages).toContainEqual({ + role: "tool", + tool_call_id: "call_image", + content: "Image attached.", + }) + expect(prepared.body.messages.at(-1)).toEqual({ + role: "user", + content: [{ type: "image_url", image_url: { url } }], + }) + }), + ) + it.effect("rejects non-image media that cannot be lowered", () => Effect.gen(function* () { const error = yield* compileRequest(