Send file DataContent as a data URI in OpenAI chat requests#559
Open
PratikDhanave wants to merge 1 commit into
Open
Send file DataContent as a data URI in OpenAI chat requests#559PratikDhanave wants to merge 1 commit into
PratikDhanave wants to merge 1 commit into
Conversation
The Chat Completions user-message builder put the raw base64 payload in a file content part's file_data, but the API expects a full data URI (data:<mediatype>;base64,<data>). The image branch in the same function already uses c.URI(), and the Responses provider sends file_data as c.URI() too — the chat file branch was the outlier, so PDFs and other non-image/non-audio files were rejected or mishandled. Use c.URI() for the file part. The adjacent audio branch keeps raw base64, which the input_audio API genuinely expects.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes how message.DataContent files (non-image/non-audio, e.g. PDFs) are serialized into OpenAI Chat Completions requests by sending file_data as a full RFC 2397 data URI (data:<mediatype>;base64,<data>), aligning with the existing image handling and the Responses provider behavior.
Changes:
- Update Chat Completions message building to use
DataContent.URI()forfile_datain the non-image/non-audio file branch. - Add a non-streaming chat test that asserts PDF
DataContentis sent as a data URI (not raw base64).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| provider/openaiprovider/chat.go | Switch non-image/non-audio DataContent file_data from raw base64 to c.URI() (data URI). |
| provider/openaiprovider/chat_test.go | Add coverage ensuring PDF DataContent is emitted as a data URI in chat requests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
buildMessageParam(chat completions,RoleUser), a non-image/non-audioDataContent(e.g. a PDF) was sent with the raw base64 payload:default: contents = append(contents, openai.FileContentPart(openai.ChatCompletionContentPartFileFileParam{ FileData: openai.String(c.Data), // raw base64 — missing the data-URI prefix Filename: openai.String(c.Name), }))DataContent.Datais the raw base64 (seemessage/content.go), but the OpenAI Chat Completions API requiresfile_datato be a full data URI —data:<mediatype>;base64,<data>(per OpenAI’s PDF-files guide:"file_data": "data:application/pdf;base64,${base64String}"). Sent as raw base64, the file is rejected/mishandled.This is the outlier:
c.URI()(chat.go:410).file_dataasc.URI()(responses.go:509), asserted inresponses_test.go.DataContent.URI()returns exactlydata:<mediatype>;base64,<data>.No test covered the chat non-image
DataContentpath (only the image case), so it went unnoticed.Fix
FileData: openai.String(c.URI()). The adjacent audio branch keeps rawc.Data— theinput_audioAPI genuinely expects raw base64, so it is correct and left unchanged.Test
TestChatDataContentMessage_File_NonStreamingsends a user message with a PDFDataContentand asserts the request body’sfile_dataisdata:application/pdf;base64,cGRmZGF0YQ==. It fails on the old code (rawcGRmZGF0YQ==) and passes with the fix.