langchaingo icon indicating copy to clipboard operation
langchaingo copied to clipboard

Unable to send BinaryPart to OpenAI Completions API

Open pilsnerbeer opened this issue 1 year ago • 5 comments

Trying to directly send a image/png to OpenAI completions API. (Model: Gpt-4o / Mini)

Snippet:

		if imageData != nil {
			historyWithBinary = append(historyWithBinary, llms.MessageContent{
				Role: llms.ChatMessageTypeHuman,
				Parts: []llms.ContentPart{
					llms.BinaryPart("image/png", imageData),
				},
			})
		}

		choices, err := client.GenerateContent(context.Background(), historyWithBinary, llms.WithTools([]llms.Tool{FileWriteTool}))

Returns Error:

Error generating content: API returned unexpected status code: 400: Invalid value:
'binary'. Supported values are: 'text', 'image_ url', and 'audio_url'.

This seems to be only problematic with OpenAI. Gemini and Ollama worked fine when i tested it with the same snippet. According to https://platform.openai.com/docs/guides/vision doc sending images directly to vision API shoudl be possible so the error is not clear to me

pilsnerbeer avatar Jul 18 '24 19:07 pilsnerbeer

I have this same issue, I can workaround with:

base64Image := base64.StdEncoding.EncodeToString(jpegBytes)

then llms.ImageURLPart(fmt.Sprintf("data:image/jpeg;base64,%s", base64Image))

Not sure if BinaryPart should just be supported anyway? Ideally it would somehow?

ccrlawrence avatar Nov 13 '24 00:11 ccrlawrence

The api link does not show any mention of binary format, so what @ccrlawrence mentioned above is the way to go

filintod avatar Feb 01 '25 19:02 filintod

Bumping this for, what if I dont want to sent image files, and other files like pdf files

iamalsaher avatar Feb 09 '25 09:02 iamalsaher

Also bumping this for the same reason. We are working with PDFs.

23RoMax avatar May 08 '25 14:05 23RoMax

It would be great if we could get that PR in and released. This is an annoying problem to have to deal with.

serbanmarti avatar Jul 16 '25 15:07 serbanmarti