Skip to content

feat(embed): add default_headers support to OpenAIEmbeddingConfig#705

Draft
micmarty-deepsense wants to merge 4 commits into
mainfrom
feat/openai-embed-default-headers
Draft

feat(embed): add default_headers support to OpenAIEmbeddingConfig#705
micmarty-deepsense wants to merge 4 commits into
mainfrom
feat/openai-embed-default-headers

Conversation

@micmarty-deepsense
Copy link
Copy Markdown
Contributor

@micmarty-deepsense micmarty-deepsense commented May 7, 2026

What changes

unstructured_ingest/embed/openai.py

  • Adds default_headers: Optional[dict[str, SecretStr]] to OpenAIEmbeddingConfig
  • In get_client() and get_async_client(): extracts header values via .get_secret_value() and passes default_headers= to the OpenAI SDK constructor
  • Uses explicit dict comprehension (not model_dump) to avoid SecretStr serialization to "**********"

The OpenAI SDK's default_headers constructor param attaches extra headers to every request without interfering with Bearer token construction. This enables custom OpenAI-compatible endpoints that require additional auth headers.

Test plan

  • Unit test: verify get_client() passes extracted headers when default_headers is set
  • Unit test: verify get_client() omits default_headers kwarg when field is None
  • Manual: configure an embedder with default_headers pointing at a custom endpoint, confirm headers appear on requests

Note

Medium Risk
Medium risk because it changes how OpenAI clients are constructed and can alter outbound authentication/headers for all embedding requests when configured.

Overview
Adds optional default_headers to OpenAIEmbeddingConfig and threads it through get_client()/get_async_client() so extra per-request HTTP headers can be sent to OpenAI (or OpenAI-compatible) endpoints.

Header values are stored as SecretStr and explicitly unwrapped via .get_secret_value() before passing default_headers into the OpenAI SDK constructors.

Reviewed by Cursor Bugbot for commit 065980c. Bugbot is set up for automated code reviews on this repo. Configure here.

@micmarty-deepsense micmarty-deepsense requested a review from a team as a code owner May 7, 2026 13:04
@micmarty-deepsense micmarty-deepsense marked this pull request as draft May 7, 2026 13:12
@micmarty-deepsense micmarty-deepsense force-pushed the feat/openai-embed-default-headers branch from 5370ca7 to ed1cb6f Compare May 13, 2026 13:36
Adds an optional default_headers: dict[str, SecretStr] field to
OpenAIEmbeddingConfig. When set, the headers are extracted via
.get_secret_value() and passed as default_headers= to the OpenAI
SDK constructor, attaching them to every request.

This enables custom OpenAI-compatible endpoints that require
non-standard auth headers alongside the Bearer token.
…sync_client

Covers: no headers (kwarg absent), headers present (SecretStr unwrapped to str),
both sync and async client construction paths.
OpenAI/AsyncOpenAI are imported inside get_client/get_async_client
(TYPE_CHECKING only at module level), so patching the openai module
directly is required. Also bump version to 1.4.11 and add CHANGELOG entry.
…les-stub tests

api_key on OpenAIEmbeddingConfig becomes Optional[SecretStr] with default None.
When unset, get_client and get_async_client construct the OpenAI SDK with the literal
placeholder "ignored" so the wire shows Authorization: Bearer ignored, which
OpenAI-compatible gateways that authenticate via default_headers can disregard.

Existing default_headers unit tests previously called mocker.patch("openai.OpenAI"),
which fails in the test_ingest_unit job because the openai package is an optional
extra and is not installed in the unit-test environment. Switched the tests to a
fake_openai fixture that injects a stub openai module into sys.modules so the
local-import code path in get_client / get_async_client can be exercised without
the real SDK. Added two more tests covering the api_key=None placeholder path
for both sync and async clients.
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.

1 participant