Skip to content

Expose request headers - #261

Open
erikos wants to merge 2 commits into
mainfrom
expose-request-headers
Open

erikos wants to merge 2 commits into
mainfrom
expose-request-headers

Conversation

@erikos

@erikos erikos commented Aug 13, 2026

Copy link
Copy Markdown

Expose request headers to pipeline wrapper run methods (opt-in)

Closes #251.

run_chat_completion / run_response, and their async variants, can now receive
the incoming request headers by declaring an optional headers parameter. The
OpenAI router forwards them only when the method declares the parameter, or
**kwargs, so existing wrappers are unaffected.

This removes the need for custom middleware plus a ContextVar to get a header
such as Authorization into a wrapper, which is the usual approach today for
per-request identity or token forwarding.

Requires a fastapi-openai-compat release

Header forwarding has to happen in fastapi-openai-compat first, since that is
what invokes the run_completion callback: fastapi-openai-compat/pull/6. Until a version
containing it is installed, this change is inert. I have deliberately left the
fastapi-openai-compat>=1.2.0 floor alone here, so the pin can be bumped once
you have released the other side.

Question for maintainers

If a wrapper declares headers without a default, and the installed compat
version does not forward headers, the call fails with
TypeError: run_chat_completion() missing 1 required positional argument: 'headers',
surfacing as a 500. Two ways to handle it, and I do not want to pick for you:

  1. Document it. Tell wrapper authors to write
    headers: dict[str, str] | None = None. Keeps the failure visible when the
    plumbing is wrong.
  2. Pass {}. When a wrapper opts in but no headers are available, hand it an
    empty dict. The contract becomes "declare it and you always get a dict", at
    the cost of hiding a misconfiguration.

Happy to implement either.

Tests

tests/test_it_openai_headers.py deploys two wrapper fixtures through the normal
path: one declaring headers that echoes Authorization back, and one keeping
the existing signature that must be called unchanged. The end-to-end checks skip
themselves when the installed compat predates forwarding, and activate
automatically once it ships. _method_accepts_kwarg and _build_call_kwargs have
unit tests covering explicit parameters, **kwargs, wrappers that do not opt in,
unintrospectable callables, and headers=None from non-HTTP entry points.

erikos added 2 commits August 13, 2026 11:54
OpenAI-compatible run_chat_completion / run_response wrappers can now receive the incoming
request headers by declaring an optional 'headers' parameter. The OpenAI router forwards headers to
the wrapper only when the method declares the parameter (or **kwargs), so existing wrappers are
unaffected. Removes the need for custom middleware + a ContextVar to bridge request headers to a
wrapper for use cases like per-request identity/token forwarding.

Assisted-by: Claude:claude-opus-4-8
Adds tests for the behaviour introduced in the previous commit. Two wrapper
fixtures under tests/test_files/files/ deploy through the normal path: one
declares `headers` and echoes the Authorization header back, the other keeps
the existing (model, messages, body) signature and must be called unchanged.

The end-to-end checks are skipped when the installed fastapi-openai-compat
predates header forwarding, since the wrapper cannot receive headers the
router never passes on. They activate on their own once a version that
forwards them is installed.

`_method_accepts_kwarg` and `_build_call_kwargs` get unit tests, covering
explicit parameters, **kwargs, wrappers that do not opt in, callables without
an introspectable signature, and headers=None from non-HTTP entry points.

Also wraps the `_build_call_kwargs` signature to stay inside the 120-column
limit, and documents the opt-in on run_response and on both async variants,
which the previous commit only did for run_chat_completion.

Assisted-by: Claude:claude-opus-5
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.

Expose request headers (or the Request) to pipeline wrapper methods

1 participant