Skip to content

Studio: send custom headers on remote connections - #15097

Merged
danielhanchen merged 24 commits into
unslothai:mainfrom
Souravrajvi0:feature/custom-provider-headers
Nov 4, 2026
Merged

danielhanchen merged 24 commits into
unslothai:mainfrom
Souravrajvi0:feature/custom-provider-headers

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #14811

OpenAI-compatible connections (including LiteLLM) can now send extra static headers. In Connections, add one header per line:

X-LiteLLM-Session-Id: unsloth-{{CHAT_SESSION_ID}}
X-Title: Unsloth

JSON objects work too. {{CHAT_SESSION_ID}} is the open chat/thread id. Reserved names (Authorization, Cookie, hop-by-hop headers, API-key aliases) are rejected so a header cannot replace the saved credential.

Test plan

  • pytest tests/test_provider_extra_headers.py tests/test_providers_db_models.py (7 passed)
  • node --experimental-strip-types --test tests/custom-provider-api-type.test.ts (2 passed)
  • In Studio: add a custom OpenAI-compatible connection, set X-LiteLLM-Session-Id: unsloth-{{CHAT_SESSION_ID}}, save, chat, and confirm LiteLLM/proxy logs show the chat id
  • Confirm Authorization: ... is rejected on save

Before / after

A saved custom connection with two extra headers, opened for editing in Settings > Connections (merge base be64e0d78 vs head db5ea11c6). After the change the form shows the Extra headers field with the saved values, the API returns them, and a local upstream receives X-Title: Unsloth on the connection check; before, none of these exist.

Settings > Connections edit form, before and after

Fixes unslothai#14811

Connections can store extra request headers (Name: value, or JSON) and
send them on every upstream call. Values may include {{CHAT_SESSION_ID}}
for the open chat. Auth and hop-by-hop names are rejected so a header
cannot replace the saved key.
@danielhanchen danielhanchen added affects UI# important# new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard labels Oct 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member

The (Python 3.13, g-r) failure here comes from this PR. It passes on main and fails on this branch locally:

tests/test_openai_container_crud.py::test_external_chat_explicit_key_honors_edited_target
routes/inference.py:30686: in _proxy_to_external_provider
    config = saved_provider_snapshot or await asyncio.to_thread(
E   AssertionError: explicit keys must not bind saved metadata

That test pins a rule: when the request carries an explicit API key, the proxy must not load the saved provider record, because the edited target in the request wins over what is stored. The custom headers path now reads the saved provider in that case too. Taking the headers from the request when a key is explicit, and from the saved record only when it isn't, should keep the rule and still send them.

Souravrajvi0 and others added 5 commits October 24, 2026 08:32
Do not look up a saved connection when the request already carries an API key.
Keep the reserved-name parser off the first-screen JS. CI was 0.5 KB
transfer over the startup budget because that code sat in the eager
external-providers module.
Main added a base-URL helper next to the same import. Move ours so
the pull request still merges.
An explicit API key must not load the saved connection. Take extra
headers from the chat request in that case, and from the stored record
only when the request uses the saved key.
@danielhanchen

Copy link
Copy Markdown
Member

Two more failures from this PR on its latest push:

  1. Core (HF=default + TRL=default):

    test_studio_evaluated_unions_do_not_grow
    36 studio files now evaluate PEP 604 unions on the floor, against 35 recorded.
    Add `from __future__ import annotations` to these, do not raise the ratchet:
      studio/backend/models/providers.py
    

    The new X | None annotation in models/providers.py is evaluated at import on the Python floor. Adding from __future__ import annotations at the top of that file clears it.

  2. Frontend build + bundle sanity: transfer 2039.8 KB > 2039.7 KB, raw 6770.9 KB > 6770.5 KB. Main currently measures 2038.3 KB / 6763.5 KB, so this PR adds about 1.5 KB transfer and 7.4 KB raw to the startup chunks. Either load the new header editor on use, or raise BUDGET in studio/frontend/scripts/check-bundle-budget.ts with a one-line reason.

Add postponed evaluation in providers.py so PEP 604 annotations do not
count against the studio import floor. Sync the frontend startup budget
with main and raise it for the connections extra-headers editor (unslothai#15097).
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen Re the (Python 3.13, g-r) / test_external_chat_explicit_key_honors_edited_target failure: when the request carries an explicit encrypted_api_key, we no longer load saved provider metadata for headers. extra_headers is taken from payload.extra_headers on that path, and from the saved record only on the DB-backed path without an explicit key.

That keeps the "explicit keys must not bind saved metadata" rule while still sending custom headers when the client supplies them.

@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen Re the other two items on the latest push:

  1. Union ratchet — added from __future__ import annotations at the top of studio/backend/models/providers.py (no ratchet bump).
  2. Bundle budget — raised startup BUDGET for the connection header editor / helpers (rawBytes: 6_940_501, transferBytes: 2_090_401, with notes for Studio: send custom headers on remote connections #15097 on top of Studio: language and platform tabs for the API page code examples #15084 after merging main).

Also cleaned up merge conflict markers in check-bundle-budget.ts on bff3d73d1b. Waiting on Core + Frontend build + bundle sanity on the latest push.

@danielhanchen

Copy link
Copy Markdown
Member

The latest merge of main into this branch did not land cleanly. The PR now changes 26 files under .github/ that it never meant to touch. They are older versions of workflows main has updated since, codeql.yml among them (main runs it weekly and on dispatch only; this branch puts back the push and pull request triggers). That is why workflow-trigger lint fails here, and both tests pass on main:

tests/studio/test_main_runs_survive_merge_bursts.py::test_a_push_never_replaces_a_nightly_or_a_dispatch_on_main
    {'codeql.yml': ['schedule', 'workflow_dispatch']} share the push group on main with ...
tests/studio/test_workflow_guards_run_unfiltered.py::test_every_workflow_reading_guard_runs_in_the_unfiltered_job

Taking main's version of everything under .github/ (git checkout origin/main -- .github, then commit) should leave only this PR's own frontend changes in the diff. The earlier comments here (the providers.py union and the startup bundle budget) still apply after that.

The last merge of main left older copies of .github/workflows on this
branch, including push and pull_request triggers on codeql.yml. Take
main's .github tree so workflow-trigger lint matches main.

Deferred annotations stay in models/providers.py. The connection header
editor fits inside main's current startup bundle budget (6790.3 KB raw,
2046.0 KB transfer, 99 chunks), so that budget is left as main set it.
Resolve conflicts in external_provider stream_chat_completion (keep
header template vars with main's docstring) and external-providers.ts
(keep extraHeadersFromSaved with main's max-tokens comment).
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Merged latest upstream/main again (head a4930e12d3). Clean merge, no conflict markers; feature tests still pass locally. PR is merge-clean (MERGEABLE); CI re-running.

@danielhanchen danielhanchen self-assigned this Nov 4, 2026
Hide header values of an owner-shared connection from other accounts, and
send saved headers on test/models calls only when the request targets the
saved endpoint without its own key.

[skip ci]
A non-ASCII or control-character value was saved, then every request to the
connection failed with UnicodeEncodeError.

[skip ci]
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T16:16:01.145704Z fb1b8f0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 4, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Trim surrounding whitespace h11 refuses, percent-encode substituted thread
ids and model names httpx cannot send, and send the saved OpenAI connection
headers on container list/create/delete.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Two connections on one URL and key that route by header no longer share a
served context window, chat template or vLLM probe.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…strictly

The served context window probe now sees the chat session id, and a JSON
headers draft is rejected instead of silently losing entries.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 4, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: fb1b8f06be

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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

Labels

affects UI# important# new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Allow custom headers for remote providers

2 participants