Skip to content

fix(agent-server): restore subscription credentials in pre-flight validation - #4898

Merged
neubig merged 2 commits into
OpenHands:mainfrom
neubig:fix/validate-profile-subscription-creds
Sep 8, 2026
Merged

neubig merged 2 commits into
OpenHands:mainfrom
neubig:fix/validate-profile-subscription-creds

Conversation

@neubig

@neubig neubig commented Sep 8, 2026

Copy link
Copy Markdown
Member

HUMAN:

Fixing bug where ChatGPT subscription profile pre-flight validation fails with "Incorrect API key provided: None".


AGENT:

Why

When a user connects their ChatGPT subscription (via OAuth) and tries to save an LLM profile using a subscription model (e.g. gpt-6-astra), the pre-flight validation fails with:

litellm.AuthenticationError: AuthenticationError: OpenAIException - {
  "error": {
    "message": "Incorrect API key provided: None.",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}

The validate_profile endpoint deserialized the LLM via plain Pydantic but never called create_subscription_llm_from_config() — unlike LLM.from_persisted() which does. The frontend sends auth_type=subscription without the OAuth access token (it lives in the credential store at ~/.openhands/auth/openai_oauth.json). Without create_subscription_llm_from_config, the LLM's is_subscription flag stayed False and _subscription_credentials stayed None, so litellm sent api_key=None to OpenAI.

Summary

  • Call create_subscription_llm_from_config(llm) in validate_profile when auth_type == "subscription", mirroring the LLM.from_persisted() path
  • Added regression test test_validate_profile_subscription_restores_credentials that verifies the OAuth access token is resolved from the credential store and passed as the API key

Issue Number

Closes #4896

How to Test

  1. Start the agent-server from this branch:
    cd ~/work/software-agent-sdk
    uv tool uvx --reinstall --from openhands-agent-server --with-editable openhands-sdk --with-editable openhands-tools --with-editable openhands-workspace --with posthog>=6,<7 agent-server --import-modules canvas_ui_tool --host 127.0.0.1 --port 18000
  2. Connect a ChatGPT subscription via the OpenHands UI (OAuth device-code flow)
  3. Try to save a new LLM profile with model gpt-6-astra and auth type "subscription"
  4. The pre-flight validation should succeed (previously failed with "Incorrect API key provided: None")

Alternatively, run the unit test:

uv run pytest tests/agent_server/test_profiles_router.py::test_validate_profile_subscription_restores_credentials -xvs

I also verified the fix end-to-end on the running agent-server:

$ curl -sf -X POST http://localhost:8000/api/profiles/gpt-6-astra-sub/validate \
    -H "Content-Type: application/json" \
    -H "X-Session-API-Key: $API_KEY" \
    -d '{"llm":{"model":"openai/gpt-6-astra","auth_type":"subscription","subscription_vendor":"openai","stream":true,"drop_params":true,"native_tool_calling":true,"is_subscription":false},"include_secrets":true}'
{"valid":true,"error":null}

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

…idation

The validate_profile endpoint deserialized the LLM via plain Pydantic but
never called create_subscription_llm_from_config(), unlike LLM.from_persisted().
When saving a new ChatGPT subscription profile, the frontend sends
auth_type=subscription without the OAuth access token (it lives in the
credential store). Without create_subscription_llm_from_config, the LLM's
is_subscription flag stayed False and _subscription_credentials stayed None,
so litellm sent api_key=None to OpenAI, failing with:

  "Incorrect API key provided: None"

Fix: call create_subscription_llm_from_config(llm) when auth_type ==
subscription, mirroring the from_persisted path.

Closes OpenHands#4896
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

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

@neubig
neubig requested a review from all-hands-bot September 8, 2026 04:03
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🤖 OpenHands is reviewing this PR.

Head commit: 7f19e158e0d0b3cf28411e9172f125288b9011d1
View the conversation: https://oss-agent-canvas.ngrok.dev/conversations/f748a4d3-783b-4052-9089-ab2baab65939

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

@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 created by an AI agent (OpenHands) on behalf of the repository maintainers.

🟡 Acceptable — the credential restoration is the right fix, but its failure path is outside the endpoint's validation contract.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟡 MEDIUM
    The normal path is narrowly scoped and covered, but missing/expired credentials can turn validation into a 500 and synchronous OAuth refresh can block the FastAPI event loop.

VERDICT:
❌ Needs rework: move credential restoration into the handled validation path and off the event loop.

KEY INSIGHT:
Credential restoration is part of pre-flight validation and needs the same async/error boundary as the provider call.


Improve this review? If any feedback above seems incorrect or irrelevant to this repository, you can teach the reviewer to do better:

  1. Add a .agents/skills/custom-codereview-guide.md file to your branch (or edit it if one already exists) with the /codereview trigger and the context the reviewer is missing (e.g., "Security concerns about X do not apply here because Y"). See the customization docs for the required frontmatter format.
  2. Re-request a review - the reviewer reads guidelines from the PR branch, so your changes take effect immediately.
  3. When your PR is merged, the guideline file goes through normal code review by repository maintainers.

Resolve with AI? Install the iterate skill in your agent and run /iterate to automatically drive this PR through CI, review, and QA until it's merge-ready.

Was this review helpful? React with 👍 or 👎 to give feedback.

…y block

Address review feedback from all-hands-bot:
- Move create_subscription_llm_from_config call inside the try/except so
  credential errors (missing/expired) return ValidateProfileResponse(valid=False)
  instead of a 500
- Wrap the synchronous factory with asyncio.to_thread to avoid blocking the
  FastAPI event loop during token refresh
- Add test for the missing-credentials failure path

Co-authored-by: openhands <openhands@all-hands.dev>
@neubig
neubig requested a review from all-hands-bot September 8, 2026 04:15
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🤖 OpenHands is reviewing this PR.

Head commit: ea3861d5d143e8b617ecf61c26ea576c21980a8d
View the conversation: https://oss-agent-canvas.ngrok.dev/conversations/1811f1a9-a460-411a-80de-6e9e946b319f

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

@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 created by an AI agent (OpenHands) on behalf of the repository maintainers.

🟢 Good taste — no material findings. The credential restoration now runs inside the endpoint’s handled validation boundary and off the event loop, addressing the previous review. The focused success and missing-credential tests pass locally.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW
    This is a narrow agent-server pre-flight fix with no REST schema change. It reuses the existing subscription restoration factory and covers both successful credential restoration and the missing-credential failure response.

VERDICT:
✅ Worth merging: Core logic is sound and the regression is covered.

KEY INSIGHT:
Pre-flight validation now reconstructs subscription runtime state through the same path used for persisted LLM profiles.

@neubig
neubig requested a review from all-hands-bot September 8, 2026 04:19
@all-hands-bot
all-hands-bot removed their request for review September 8, 2026 04:19
@neubig

neubig commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

Thanks for the positive review! Could you please submit a formal approval (APPROVED) so the merge gate can be satisfied? The review mentioned 'Worth merging' with no material findings. Tagging @all-hands-bot for re-review.

@neubig
neubig requested a review from all-hands-bot September 8, 2026 04:21

@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.

Approved. The latest revision addresses the prior review by restoring subscription credentials inside the handled validation boundary and off the event loop. The focused success and missing-credential tests pass locally, lint passes, and the current CI suite is green.

Generated by OpenHands AI on behalf of the user.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

Submitted a formal APPROVED review on the current head (ea3861d5d143e8b617ecf61c26ea576c21980a8d). I also re-ran the two focused subscription validation tests and Ruff locally; all passed, and the current GitHub checks are green.

Generated by OpenHands AI on behalf of the user.

@neubig
neubig merged commit 3896f18 into OpenHands:main Sep 8, 2026
49 of 50 checks passed
Bowbee pushed a commit to PlayCastDotIo/openhands-software-agent-sdk that referenced this pull request Sep 15, 2026
…idation (OpenHands#4898)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Pre-flight validation fails for ChatGPT subscription profiles with "Incorrect API key provided: None"

3 participants