Skip to content

fix(tui): use role=user for model switch marker to avoid HTTP 400 on strict providers - #48361

Closed
Elshayib wants to merge 2 commits into
NousResearch:mainfrom
Elshayib:fix/model-switch-marker-role-user
Closed

fix(tui): use role=user for model switch marker to avoid HTTP 400 on strict providers#48361
Elshayib wants to merge 2 commits into
NousResearch:mainfrom
Elshayib:fix/model-switch-marker-role-user

Conversation

@Elshayib

Copy link
Copy Markdown
Contributor

Problem

_append_model_switch_marker in tui_gateway/server.py injects a {"role": "system", ...} entry into the conversation history mid-session after a live model switch. Strict providers (vLLM, Qwen) reject any system role message that isn't the first message in the conversation, returning HTTP 400.

Root Cause

The marker is an in-conversation annotation informing the model about a runtime switch -- it is NOT a true system prompt. Using role: "system" violates the strict message ordering requirements of these providers.

Fix

Changed role: "system" to role: "user" at all 3 sites in _append_model_switch_marker:

  1. In-memory session history entry
  2. db.append_message() via agent's session DB
  3. scoped_db.append_message() via fallback session DB path

Tests

  • Updated existing test assertions in test_config_set_model_switches_agent_without_touching_env to expect role: "user"
  • Added TestAppendModelSwitchMarker regression class (4 tests):
    • test_marker_uses_user_role_not_system - verifies role and content
    • test_marker_without_provider - verifies no "via provider" when provider is empty
    • test_marker_no_session - no-op when session is None
    • test_marker_empty_session_key - no-op when session_key is empty

All 282 tests in tests/test_tui_gateway_server.py pass.

Fixes #48338

islam666 and others added 2 commits June 18, 2026 09:40
…l picker

When the current provider is a custom endpoint (custom or custom:*),
the model switch pipeline must NOT auto-switch to a native provider
or OpenRouter based on static catalog matches. The user explicitly
configured their own endpoint — the same model name may be served
there, and silently rewriting model.provider destroys their config.

Changes:
- detect_static_provider_for_model(): skip static catalog scan when
  current provider is custom/custom:*
- switch_model() step e guard: extend is_custom to cover custom:*
  providers, preventing detect_provider_for_model() last-resort
  OpenRouter fallback from firing
- _persist_model_switch(): use load_config() instead of _load_cfg()
  for a fully merged config that preserves all sections on save

Fixes #48305

Co-authored-by: Islam Elshayib <islam.elshayib@gmail.com>
…strict providers

_append_model_switch_marker injected role:"system" mid-conversation,
which violates strict provider requirements (vLLM, Qwen) that only
allow system as the first message. The marker is an in-conversation
annotation, not a true system prompt, so role:"user" is correct.

Changes:
- tui_gateway/server.py: entry role changed from "system" to "user"
  in _append_model_switch_marker (3 sites: in-memory history + 2 DB
  append_message calls)
- tests/test_tui_gateway_server.py: updated existing test assertions
  from role=system to role=user; added TestAppendModelSwitchMarker
  regression class (4 tests)

Fixes #48338
@alt-glitch alt-glitch added type/bug Something isn't working comp/tui Terminal UI (ui-tui/ + tui_gateway/) comp/gateway Gateway runner, session dispatch, delivery comp/cli CLI entry point, hermes_cli/, setup wizard provider/qwen Qwen / Alibaba Cloud (OAuth) P2 Medium — degraded but workaround exists labels Jun 18, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged the model-switch marker fix via PR #54210 (commit 61622bb), using the earliest duplicate (#48346) as the base. Closing this one: the marker fix is now on main, and the bundled custom-provider catalog changes in models.py / model_switch.py are unrelated to #48338 — happy to review those as a separate focused PR if you'd like to reopen just that part. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists provider/qwen Qwen / Alibaba Cloud (OAuth) type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: _append_model_switch_marker injects role:"system" mid-conversation → HTTP 400 on strict providers (vLLM, Qwen)

3 participants