fix(filter-openai): collapse 3 system messages into one#46
Open
joryirving wants to merge 1 commit into
Open
Conversation
Many strict chat templates (e.g. Qwen3 via llama.cpp --jinja) reject requests with multiple consecutive system messages and raise 'System message must be at the beginning', returning HTTP 400. Because OpenAIFilterer.Filter returns FilterOnFailure (default true) on any error, every message was silently dropped from the pipeline. Mirror the annotator pattern: concatenate the framing prompt, the user prompt, and the closing instructions into a single leading system message. The user message remains unchanged. Fixes tyzbit#45 Adds a regression test (filter_openai_test.go) that captures the outgoing request body via httptest and asserts exactly one system message plus one user message, in the correct order. Also pins the existing default behavior (FilterOnFailure=true) and the backend-error -> silent drop path described in the issue.
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.
Summary
OpenAIFilterer.Filterbuilds its chat-completions request with three consecutive system messages (framing prompt, user prompt, closing instructions) followed by the user message. Strict chat templates (e.g. Qwen3 served byllama.cppwith--jinja, hardened Llama-3 instruct templates, and others) only allow a single leading system message and raise on anything else. The backend returns HTTP 400 before the model runs, and becauseOpenAIFilterer.Filterreturnso.FilterOnFailureon any error — andFilterOnFailuredefaults totrue— every message is silently dropped from the pipeline.The sibling
OpenAIAnnotatoralready does this correctly: it concatenates the framing prompt, user prompt, and closing instructions into a single system message. This PR makes the filter mirror that pattern.Fixes #45
Changes
filter_openai.go— collapseOpenAISystemPrompt + o.UserPrompt + OpenAIFinalInstructionsinto oneSystemMessage, matchingannotator_openai.go.filter_openai_test.go(new) — regression test that captures the outgoing request body viahttptest.NewServerand asserts:Bearer <apiKey>./v1/chat/completions.Also pins the existing behavior called out in the issue:
FilterOnFailurestruct-tag default istrue(verified viamcuadros/go-defaults).FilterOnFailure: true→ message is dropped (the silent-drop path).Impact
llama.cpp --jinjawith Qwen3 (the example in the issue), hardened Llama-3 instruct templates, and others.o.UserPrompt,OpenAISystemPrompt, andOpenAIFinalInstructionsremain user-overridable; they just get concatenated.Verification
The new test was also confirmed to fail when run against the un-patched code: it sees 3 system + 1 user messages and fails the count assertion.
Out of scope (intentionally)
FilterOnFailure: truedefault or making the silent-drop behavior more visible (e.g. emitting a metric/alert) — separate concern, happy to file a follow-up issue.