Skip to content

fix: serialize single-text message content as string for OpenAI-compatible APIs#342

Open
octo-patch wants to merge 1 commit into
plandex-ai:mainfrom
octo-patch:fix/issue-256-string-content-for-openai-compatible-apis
Open

fix: serialize single-text message content as string for OpenAI-compatible APIs#342
octo-patch wants to merge 1 commit into
plandex-ai:mainfrom
octo-patch:fix/issue-256-string-content-for-openai-compatible-apis

Conversation

@octo-patch

Copy link
Copy Markdown

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 with content as 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:

'messages.0' → For 'role:system' the following must be satisfied
  [('messages.0.content' → Value must be a string)]

The root cause is that ExtendedChatMessage.Content is typed as []ExtendedChatMessagePart, which always marshals to a JSON array. The ToOpenAI() 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 through json.Marshal(extendedReq) which hits the default array serialization.

Solution

Add a MarshalJSON method on ExtendedChatMessage that mirrors the logic in ToOpenAI():

  • Single plain-text part, no cache_control, no image_url → emit content as a JSON string (compatible with Groq, Ollama, and the OpenAI spec).
  • Multiple parts, or Anthropic cache_control/image_url present → emit content as 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

  • Verified the server builds cleanly (go build ./...).
  • The fix mirrors the already-tested ToOpenAI() path; the serialization logic is straightforward and covered by the existing code structure.

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When using custom Groq API

1 participant