Skip to content

fix: normalize chat completions system messages#458

Draft
MoeMion wants to merge 1 commit into
DEEIX-AI:devfrom
MoeMion:codex/normalize-chat-completions-system-messages
Draft

fix: normalize chat completions system messages#458
MoeMion wants to merge 1 commit into
DEEIX-AI:devfrom
MoeMion:codex/normalize-chat-completions-system-messages

Conversation

@MoeMion

@MoeMion MoeMion commented Jul 10, 2026

Copy link
Copy Markdown

Root cause

Chat-completions-compatible providers can reject or inconsistently interpret requests containing multiple system messages at different positions. File context, tool guidance, and final synthesis instructions could therefore produce an invalid or ambiguous message sequence.

Impact

OpenAI Chat Completions and OpenRouter Chat requests are affected. Non-system messages, including tool calls and tool results, keep their original order and payload.

Changes

  • Merge all non-empty system message content into one leading system message, separated by blank lines.
  • Preserve the order of all non-system messages.
  • Add regression tests for OpenAI Chat Completions and OpenRouter Chat, including tool-call payload preservation.

Validation

  • go test ./internal/infra/llm
  • go vet ./internal/infra/llm
  • gofmt -d backend/internal/infra/llm/openai_chat_completions.go backend/internal/infra/llm/request_params_test.go
  • git diff --check upstream/dev...HEAD

Full make test was attempted, but unrelated SQLite packages could not compile in the local Windows CGO environment because sqlite3.h is unavailable.

@MoeMion

MoeMion commented Jul 10, 2026

Copy link
Copy Markdown
Author

API returned

{
  "error": {
    "code": "400",
    "message": "System message must be at the beginning.",
    "type": "BadRequestError"
  }
}

when system prompt not at beginning.

@github-actions

Copy link
Copy Markdown

This pull request has had no activity for 7 days. It will be closed in 7 days if there is no further activity.

@github-actions github-actions Bot added the stale label Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant