Skip to content

Add default_headers to the OpenAI provider - #548

Open
domondi1 wants to merge 2 commits into
google:mainfrom
domondi1:feat/openai-default-headers
Open

domondi1 wants to merge 2 commits into
google:mainfrom
domondi1:feat/openai-default-headers

Conversation

@domondi1

@domondi1 domondi1 commented Oct 3, 2026 •

Copy link
Copy Markdown

Description

OpenAILanguageModel builds its openai.OpenAI client from api_key, base_url and organization only, and extra kwargs go to the Chat Completions call, so there's no way to send HTTP headers with every request (needed by many OpenAI-compatible proxies and gateways). This adds an optional default_headers argument that is passed to the client, reachable via language_model_params:

lx.extract(
    ...,
    model_id="gpt-4o-mini",
    language_model_params={
        "base_url": "http://localhost:8000/v1",
        "default_headers": {"X-Run-Id": "job-001"},
    },
)

None by default, so nothing changes for existing users. Headers go to openai.OpenAI(default_headers=...), not into the request params.

Fixes #549

Feature

How Has This Been Tested?

New tests in tests/inference_test.py: headers reach the client and not the API params; unset by default. Also checked that factory.create_model with provider_kwargs={"default_headers": ...} reaches the client.

$ pytest tests --ignore=tests/test_live_api.py
$ pylint --rcfile=tests/.pylintrc tests/inference_test.py   # 10.00/10
$ pyink / isort on the changed files

Note on CI: lint-tests currently reports W0223 (abstract-method) in tests/schema_test.py, a file this PR doesn't touch; the same warnings appear on main with the current pylint/astroid, so it looks like a tool version change rather than this diff.

Checklist:

  • I have read and acknowledged Google's Open Source
    Code of conduct.
  • I have read the
    Contributing
    page, and I either signed the Google
    Individual CLA
    or am covered by my company's
    Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked
    issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked
    issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed
    Google's Python Style Guide
    and ran pylint over the affected code.

Disclosure: I maintain Inferrail, an open-source OpenAI-compatible gateway that uses such headers for per-job cost limits; the change itself is generic.

Signed-off-by: domondi1 <91575411+domondi1@users.noreply.github.com>
@github-actions github-actions Bot added the size/XS Pull request with less than 50 lines changed label Oct 3, 2026
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

No linked issues found. Please link an issue in your pull request description or title.

Per our Contributing Guidelines, all PRs must:

  • Reference an issue with one of:
    • Closing keywords: Fixes #123, Closes #123, Resolves #123 (auto-closes on merge in the same repository)
    • Reference keywords: Related to #123, Refs #123, Part of #123, See #123 (links without closing)
  • The linked issue should have 5+ 👍 reactions from unique users (excluding bots and the PR author)
  • Include discussion demonstrating the importance of the change

You can also use cross-repo references like owner/repo#123 or full URLs.

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 9 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@BichengWang BichengWang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed head 24322b1c73fcee548fe660523e3deb8a5dba59d4.

[P2] Preserve the existing positional constructor arguments when adding default_headers (signature). Inserting it before openai_schema shifts every existing positional argument from the fifth onward. For example, this previously valid call now raises TypeError: 'OpenAISchema' object is not iterable at dict(default_headers), before client creation:

from unittest import mock
from langextract.providers.openai import OpenAILanguageModel
from langextract.providers.schemas.openai import OpenAISchema

schema = OpenAISchema({"type": "object", "properties": {}})
with mock.patch("openai.OpenAI"):
    model = OpenAILanguageModel("gpt-4o-mini", "test-key", None, None, schema)
    assert model.openai_schema is schema

Please place the new argument after the existing max_workers parameter (optionally making only the new argument keyword-only), and add a regression test for the old positional signature. I checked both the fifth-position schema and the full eight-position constructor on the PR parent 62b933a2c757fd2bbb100498571b8d1692db4344; both pass there. Moving the new argument after max_workers in memory restores both at this head.

Validation at the exact PR head: Python 3.11.17, OpenAI SDK 3.31.0; 878 passed, 24 deselected with python -m pytest tests -m "not live_api and not requires_pip and not integration". Full source/test isort, Pyink, Pylint (using the separate source/test configs), and all three import contracts passed. Dependencies were installed from the local cache; dotenv loading was disabled and provider credentials removed for the suite. No live APIs, pip-install integration, other Python versions, or merge with current main were tested.

@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 14 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

1 similar comment
@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 14 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 17 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 27 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

1 similar comment
@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 27 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

This branch has not been deployed

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

Labels

size/XS Pull request with less than 50 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: default_headers for the OpenAI provider

2 participants

⌂ 换网址