fix: serialize single-text message content as string for OpenAI-compatible APIs#342
Open
octo-patch wants to merge 1 commit into
Conversation
…tible APIs Some OpenAI-compatible providers (Groq, and others) require the `content` field on system messages to be a plain JSON string, not an array. Previously ExtendedChatMessage always marshaled content as an array, causing a 400 error: 'messages.0.content' → Value must be a string Add a MarshalJSON method that emits content as a string when the message has exactly one plain-text part with no cache_control or image_url, preserving the array form only when multiple parts or Anthropic-style extensions are present. Fixes plandex-ai#256 Co-Authored-By: Octopus <liyuan851277048@icloud.com>
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.
Fixes #256
Problem
When using a custom OpenAI-compatible provider (e.g. Groq at
https://api.groq.com/openai/v1), the server sends the request body withcontentas a JSON array even for simple system/user messages that contain a single text part. Many OpenAI-compatible providers are strict about the OpenAI spec and reject this with a 400 error:The root cause is that
ExtendedChatMessage.Contentis typed as[]ExtendedChatMessagePart, which always marshals to a JSON array. TheToOpenAI()method already handled this correctly (it returns a plain string when there is exactly one text part), but that path is only used for direct OpenAI API calls. Requests to all other providers go throughjson.Marshal(extendedReq)which hits the default array serialization.Solution
Add a
MarshalJSONmethod onExtendedChatMessagethat mirrors the logic inToOpenAI():contentas a JSON string (compatible with Groq, Ollama, and the OpenAI spec).cache_control/image_urlpresent → emitcontentas the existing array (required for Anthropic prompt caching and vision).This keeps full backward compatibility: Anthropic-style cache-control markers and multimodal content continue to use the array form, while simple text messages use the string form expected by standard OpenAI-compatible APIs.
Testing
go build ./...).ToOpenAI()path; the serialization logic is straightforward and covered by the existing code structure.