Skip to content

fix(chat): apply system prompts once per request - #3864

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:codex/fix-3510-system-prompt-owner
Sep 24, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:codex/fix-3510-system-prompt-owner

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Custom system prompts no longer reach the model twice or retain the old persona after an update. The SDK owns per-request prompt assembly and preserves the native system-message channel for local and cloud providers.

Fixes #3510.

Test plan:

  • System-prompt and tool-message suites: 23 passed, inspecting real Lemonade/Claude/OpenAI request shaping, runtime updates, RAG history, per-call overrides, and assistant-name changes.
  • Live Lemonade send returned ALPHA; streaming after a prompt update returned BETA, matching the respective instructions.
  • Black, isort, whitespace checks, code and architecture review passed.

The full agent benchmark has not been rerun; live validation targets the changed SDK behavior.

@github-actions github-actions Bot added documentation Documentation changes chat Chat SDK changes tests Test changes labels Sep 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Approve with suggestions

A custom system prompt was being applied twice — once by the SDK when it builds the request, once again by the provider client that held its own copy — and an updated prompt left the old persona in place. Dropping the provider-held copy is the right fix: the SDK is the only layer that sees per-request overrides, so it should be the only layer that inserts the prompt. The new tests are genuine regression tests (they fail against the old code) and cover all four send paths plus both update routes.

One thing worth a look before merge, not a blocker: the fix is verified only against the local Lemonade path. For a library user running the SDK against Claude or OpenAI with a configured prompt, the prompt used to reach those services through their dedicated system-prompt channel; now it only survives as text inside the user message. It still reaches the model, so nothing breaks, but it's a quieter change than the PR describes and no test covers it. Worth either confirming that's intended or following up.

Real-world evidence

No evidence bundle was produced for this run (the automated stage didn't write one). The PR description reports a live Lemonade persona check returning ALPHA before the update and BETA after, which is exactly the right check for this change, but the transcript isn't pasted — consider inlining the two-line output so a reviewer can see it. The change touches the SDK/CLI surface, not the Agent UI, so no screenshot is expected. My verdict otherwise rests on static review plus reading the new tests; I could not execute them here (no pytest in this environment).

🔍 Technical details

🟡 Cloud providers lose their native system channel on the send() / send_stream() path (src/gaia/chat/sdk.py:110)

send() and send_stream() go through llm_client.generate(full_prompt), where full_prompt already embeds the system prompt via Prompts.format_chat_history(). With system_prompt=None on the client:

  • Lemonade — correct, and the duplication is gone.
  • Claude — ClaudeProvider._build_params extracts system from system-role messages and otherwise falls back to self._system_prompt (providers/claude.py:350). With no system-role message and no stored prompt, system is now None; the persona only arrives as Gemma-templated text inside a user message.
  • OpenAI — same shape (providers/openai_provider.py:53).

send_messages() / send_messages_stream() are unaffected — they prepend a real system-role message, which both providers honor. The structural fix is to route send() through the structured-message builder rather than Prompts.format_chat_history(); short of that, a test that patches the Claude/OpenAI provider and asserts the outgoing system parameter would pin the intended behavior. The new fixture only patches gaia.llm.providers.lemonade.LemonadeClient, so all ten cases exercise one provider.

Note this does not affect agents: Agent.__init__ deliberately leaves system_prompt unset in AgentConfig and passes it per request (agents/base/agent.py:1059-1079), so gaia chat and every hub agent were never double-prompted.

🟢 Prompt-ownership note landed under the wrong heading (docs/sdk/sdks/chat.mdx:45)

The paragraph sits between ### AgentResponse and its own intro sentence, so it reads as a property of the response type. It belongs at the end of the AgentConfig section, right after the dataclass that declares system_prompt:

The SDK applies the system prompt once per request. `set_system_prompt()` and
`update_config(system_prompt=...)` take effect on the next request; structured
message calls can override it for one request without changing the stored prompt.

### AgentResponse

Responses include text, history, and optional statistics:

(Apply this over the added paragraph plus the ### AgentResponse heading and the sentence under it.)

Strengths

  • The update_config deletion is a real cleanup, not just dead-code removal: the if "system_prompt" in kwargs: pass block carried a comment claiming the prompt is "handled through Prompts class, not directly", which was the misconception behind the bug. The generic setattr loop above it already did the right thing.
  • The test asserts count(...) == 1 and "SYS-ORIGINAL" not in request rather than just "prompt present" — that's what makes it catch both halves of the bug (duplication and stale persona), and it inspects the request after real SDK and provider assembly instead of mocking the SDK's own output.
  • Scope is tight: two source lines, one doc paragraph, one test file. No drive-by changes.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Refreshed this branch against current main so it can merge again — the branch had fallen far enough behind that GitHub could no longer compute a merge.

The update was a clean merge: nothing was resolved by hand, and no commit on this branch was rewritten. CI now runs against an up-to-date base.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 24, 2026
Merged via the queue into amd:main with commit 9abaa0b Sep 24, 2026
32 checks passed
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 24, 2026
main is broken: `test_chat_system_prompt_owner.py` (added by amd#3864)
still imports `OpenAIProvider` directly, which amd#3899 deleted as an
intentional API break. amd#3899's own validation command never ran this
file, so the collision only showed up once both merged.

Every non-Claude cloud model now goes through `LemonadeProvider` — its
wire shape (system prompt folded into `messages[0]`) is exactly what
this test needs to exercise, so the fix swaps the mock target from a raw
OpenAI SDK client to `LemonadeProvider._backend` instead of
reintroducing the deleted class.

## Test plan
- [x] `python -m pytest tests/unit/test_chat_system_prompt_owner.py -q`
— 16 passed
- [x] `python -m pytest tests/unit/ -k "chat_sdk or system_prompt or
lemonade_provider or test_claude_provider or
test_removed_llm_providers"` — 134 passed
- [x] `git grep` confirms no other references to the removed
`OpenAIProvider`/`openai_provider` module remain

Co-authored-by: f <f@f>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chat Chat SDK changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(chat-sdk): system prompt is sent twice and update_config never applies it

2 participants