Skip to content

fix(agent-server): resolve provider connection in /switch_llm - #5417

Merged
juanmichelini merged 3 commits into
mainfrom
fix/switch-llm-resolve-provider-connection
Oct 1, 2026
Merged

juanmichelini merged 3 commits into
mainfrom
fix/switch-llm-resolve-provider-connection

Conversation

@juanmichelini

@juanmichelini juanmichelini commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

HUMAN:

Fixing a real 401 bug users hit in agent-canvas when switching to a linked OpenRouter profile — the keyless forwarded config needs server-side resolution.

Screenshots.

Before

image

After

image

AGENT:

Why

Switching a conversation to an LLM profile linked to a provider connection (e.g. an OpenRouter profile in agent-canvas) silently lost the API key. The frontend fetches the profile config without resolving secrets (they aren't exposed to the client), so it forwards a keyless config carrying only provider_connection_id. The /switch_llm handler decrypted inline secrets but never resolved the referenced connection — unlike switch_profile / LLMProfileStore.load(resolve_provider=True) — so the installed LLM ran with api_key=None and OpenRouter rejected every call with 401 Invalid API key.

Fixes OpenHands/OpenHands#17206. Tracked in this repo at #5439.

Summary

  • Added a public LLMProfileStore.resolve_provider_connection(llm, *, cipher=None) that reuses the existing _resolve_provider_connection logic with an optional profile name, so a caller-supplied LLM (no profile name) can resolve its provider_connection_id into a real api_key + base_url.
  • switch_conversation_llm now resolves the provider connection (inside store_errors()) before calling conversation.switch_llm, mirroring switch_profile. A dangling reference / missing credential surfaces as 422 before the working LLM is replaced; an inline-key config without provider_connection_id is byte-identical to the old path.
  • This makes /switch_llm and /switch_profile resolve provider connections identically.

Issue Number

Fixes #5439
Ref OpenHands/OpenHands#17206

How to Test

uv sync --group dev
uv run pytest tests/agent_server/test_conversation_router.py -k switch_conversation_llm -q
uv run pytest tests/agent_server/test_conversation_router.py tests/sdk/llm/test_llm_profile_store.py tests/agent_server/test_profiles_router.py -q
uv run ruff check openhands-agent-server/openhands/agent_server/conversation_router.py openhands-sdk/openhands/sdk/llm/llm_profile_store.py tests/agent_server/test_conversation_router.py
uv run pyright tests/agent_server/test_conversation_router.py

New regression tests in tests/agent_server/test_conversation_router.py:

  • test_switch_conversation_llm_resolves_provider_connection — a provider_connection_id is resolved into the connection's api_key before the LLM is installed (the #17206 regression).
  • test_switch_conversation_llm_resolves_encrypted_provider_connection — encrypted-at-rest credentials are decrypted with the server's cipher and the connection's base_url is applied.
  • test_switch_conversation_llm_inline_key_without_provider_connection — an inline api_key with no provider_connection_id is applied unchanged (old path preserved).
  • test_switch_conversation_llm_missing_provider_connection_rejects_before_switch — a dangling provider_connection_id (no inline key) returns 422 and does not call conversation.switch_llm, so the working LLM is not replaced.

Result: 8 switch_llm tests pass; combined run of test_conversation_router.py + test_llm_profile_store.py + test_profiles_router.py → 266 passed. Ruff + pyright clean. OpenAPI 422 media type for /switch_llm unchanged (application/json default validation response).

End-to-end reproduction (agent-canvas, manual): link an OpenRouter profile to a provider connection, switch a running conversation to it, and send a message — calls now authenticate instead of 401.


This pull request was created by an AI agent (OpenHands) on behalf of @juanmichelini.


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:53af5e9-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-53af5e9-python \
  ghcr.io/openhands/agent-server:53af5e9-python

All tags pushed for this build

ghcr.io/openhands/agent-server:53af5e9-golang-amd64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-golang-amd64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-golang-amd64
ghcr.io/openhands/agent-server:53af5e9-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:53af5e9-golang-arm64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-golang-arm64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-golang-arm64
ghcr.io/openhands/agent-server:53af5e9-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:53af5e9-java-amd64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-java-amd64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-java-amd64
ghcr.io/openhands/agent-server:53af5e9-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:53af5e9-java-arm64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-java-arm64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-java-arm64
ghcr.io/openhands/agent-server:53af5e9-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:53af5e9-python-amd64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-amd64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-amd64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-amd64
ghcr.io/openhands/agent-server:53af5e9-python-arm64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-arm64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-arm64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-arm64
ghcr.io/openhands/agent-server:53af5e9-python-minimal-amd64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-minimal-amd64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-minimal-amd64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:53af5e9-python-minimal-arm64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-minimal-arm64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-minimal-arm64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:53af5e9-python-slim-amd64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-slim-amd64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-slim-amd64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:53af5e9-python-slim-arm64
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-slim-arm64
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-slim-arm64
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:53af5e9-golang
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-golang
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-golang
ghcr.io/openhands/agent-server:53af5e9-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:53af5e9-java
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-java
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-java
ghcr.io/openhands/agent-server:53af5e9-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:53af5e9-python-minimal
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-minimal
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-minimal
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-minimal
ghcr.io/openhands/agent-server:53af5e9-python-slim
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python-slim
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python-slim
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime-slim
ghcr.io/openhands/agent-server:53af5e9-python
ghcr.io/openhands/agent-server:53af5e915e50f08599f2ea6e714403a174861163-python
ghcr.io/openhands/agent-server:fix-switch-llm-resolve-provider-connection-python
ghcr.io/openhands/agent-server:53af5e9-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 53af5e9-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 53af5e9-python-amd64) are also available if needed

Switching a conversation to an LLM profile linked to a provider connection
(e.g. an OpenRouter profile in agent-canvas) lost the API key: the frontend
fetches the profile without resolving secrets and forwards a keyless config
carrying only `provider_connection_id`, and the `/switch_llm` endpoint
decrypted inline secrets but never resolved the referenced connection — so
the installed LLM ran with `api_key=None` and OpenRouter rejected calls with
401 Invalid API key.

Resolve the provider connection before installing the LLM, mirroring
`LLMProfileStore.load(resolve_provider=True)`. A dangling reference or
missing credential surfaces as 422 before the working LLM is replaced; an
inline-key config without `provider_connection_id` is byte-identical to the
old path.

Fixes OpenHands/OpenHands#17206.
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   conversation_router.py2721594%195, 324, 408, 455, 515, 696–699, 711–714, 754, 792
openhands-sdk/openhands/sdk/llm
   llm_profile_store.py167796%51–52, 143–144, 326, 358, 364
TOTAL46624829282% 

@all-hands-bot

Copy link
Copy Markdown
Collaborator

⚠️ Review paused: current-head checks did not pass

The current head bbf6172fed41 has checks that did not pass, so no review conversation was started:

  • pre-commit

Fix the checks above and push. The scheduled scan then starts the review on the updated head.

This is an automated check - no AI was used to generate this comment.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Summary

The fix targets a real bug and is well-scoped: /switch_llm now resolves a caller-supplied provider_connection_id before installing the LLM, mirroring LLMProfileStore.load(resolve_provider=True). The LLMProfileStore.resolve_provider_connection(...) entry point and the router's store_errors() wrapping are sound, and the four new regression tests pass locally (8/8 switch_llm tests). However the current head is not mergeable as-is: the newly declared 422 response removes the application/json media type from the public OpenAPI contract, which fails the required REST API (OpenAPI) check and violates the repo's 5-minor-release REST compatibility policy; the same head also fails pre-commit (pyright) on the new test.

Findings

1. New explicit 422 drops application/json from the public REST contract (blocking)

conversation_router.py lines 554-557 declare 422: {"description": "Referenced provider connection is missing"}. In FastAPI, a responses entry for a status code that is otherwise auto-generated for the operation replaces the generated entry rather than merging with it. Because switch_conversation_llm has a body parameter, FastAPI auto-adds a 422 carrying content: {"application/json": {schema: HTTPValidationError}}. The explicit entry has no content, so that media type disappears.

Verified on this head: exporting the public OpenAPI via .github/scripts/export_agent_server_openapi.py yields paths./api/conversations/{conversation_id}/switch_llm.post.responses.422 == {"description": "Referenced provider connection is missing"} with no content key, and a minimal FastAPI repro reproduces the same drop. This is exactly the CI failure reported by the REST API (OpenAPI) job (run 36798335415): removed the media type application/json for the response with the status 422. Repo policy (openhands-agent-server/AGENTS.md) requires breaking REST contract changes to keep the old contract for 5 minor releases, and the breakage script has no allowlist bucket for a removed response media type, so this is a genuine public-contract regression, not a lint nit.

Note that the existing 422 declarations in plugins_router, canvas_extensions_router, and skills_router use handlers without a body parameter, so FastAPI does not auto-generate a 422 for them and their explicit declaration is purely additive — that is why they do not break. Here it is a replacement.

Suggested direction: keep the generated content in the declared 422 (for example declare it with the HTTPValidationError content schema alongside the description), or drop the explicit 422 declaration and document the new failure mode in the endpoint docstring instead.

2. pre-commit (pyright) is red on the new test

tests/agent_server/test_conversation_router.py line 2581 (assert forwarded.api_key.get_secret_value() == "or-secret") fails pyright with Cannot access attribute "get_secret_value" for class "str" and "get_secret_value" is not a known attribute of "None" (reportOptionalMemberAccess). The pre-commit job (run 36798335523) is failing on the current head. Narrow the type first, e.g. assert isinstance(forwarded.api_key, SecretStr) before dereferencing.

Assessment

The bug fix itself is correct, minimal, and covered by good regression tests. But the current head fails two required checks, and finding 1 is a real backward-compatibility regression on the public REST API rather than a stylistic concern. Both should be addressed before merge. The linked issue OpenHands/OpenHands#17206 (labels: bug, llm, agent-canvas, priority:medium, security; no readiness label) is filed against the agent-canvas repo while the handler lives here, which the PR description already calls out.

🔄 CHANGES REQUESTED

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Summary

The fix is correct, minimal, and appropriately scoped. /switch_llm now resolves a caller-supplied provider_connection_id into the connection's api_key / base_url before installing the LLM, mirroring LLMProfileStore.load(resolve_provider=True) / switch_profile. LLMProfileStore.resolve_provider_connection(llm, *, cipher=None) reuses the existing _resolve_provider_connection logic with an optional profile name, and the router resolves inside store_errors() so a dangling reference surfaces as 422 before the working LLM is replaced. An inline-key config without provider_connection_id is unchanged.

Both findings from the prior review are addressed on this head:

  • The explicit 422 entry was removed from the route's responses, so FastAPI's default validation 422 (application/json + HTTPValidationError) is restored. I re-exported the public OpenAPI on this head and confirmed paths./api/conversations/{conversation_id}/switch_llm.post.responses.422 is again the generated application/json/HTTPValidationError shape. The REST API (OpenAPI) check now passes.
  • The pyright failure was fixed by narrowing forwarded.api_key with isinstance(..., SecretStr) before get_secret_value(). uv run pyright tests/agent_server/test_conversation_router.py reports 0 errors and the pre-commit check passes.

Verification performed on this head

  • uv run pytest tests/agent_server/test_conversation_router.py -k switch_conversation_llm -q → 8 passed.
  • uv run pytest tests/agent_server/test_conversation_router.py tests/sdk/llm/test_llm_profile_store.py tests/agent_server/test_profiles_router.py -q → 268 passed.
  • uv run pyright tests/agent_server/test_conversation_router.py → 0 errors.
  • Public OpenAPI export confirms the 422 contract is unchanged from the base.

Non-blocking note

agent-server-stress-tests is red on this head, but it is a pre-existing flake unrelated to this change. I reproduced the same failure on a clean origin/main worktree: tests/agent_server/stress/test_concurrent_conversations.py::test_concurrent_conversations_isolated_and_fast fails intermittently on the wall-clock budget assertion (concurrent wall (0.6-0.7s) > budget (~0.6s = ref × 4.0)), and occasionally on ConversationRunLimitExceeded when a semaphore permit is still held by an in-flight predecessor. The PR diff does not touch the stress fixtures, max_concurrent_runs, or the run-slot logic, so this is not a regression from this change. Worth a separate flake-fix PR, but it does not block this one.

Assessment

No material correctness, security, or compatibility defect remains on this head. The linked issue #5439 is ready-for-dev (labels: bug, llm, priority:medium, ready-for-dev, duplicate-candidate) and its acceptance criteria — resolve the linked connection's credentials on /switch_llm — are met by the implementation and covered by regression tests. The duplicate-candidate label on #5439 is worth a maintainer's eye, but it is orthogonal to the code correctness here.

✅ APPROVED

@all-hands-bot
all-hands-bot requested a review from neubig October 1, 2026 19:50
@juanmichelini
juanmichelini merged commit af016a0 into main Oct 1, 2026
66 of 67 checks passed
@juanmichelini
juanmichelini deleted the fix/switch-llm-resolve-provider-connection branch October 1, 2026 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants