mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 14:43:07 +00:00
feat(openrouter): default headers (#35369)
This commit is contained in:
@@ -152,14 +152,27 @@ class ChatOpenRouter(BaseChatModel):
|
||||
"""OpenRouter API base URL. Maps to SDK `server_url`."""
|
||||
|
||||
app_url: str | None = Field(
|
||||
default_factory=from_env("OPENROUTER_APP_URL", default=None),
|
||||
default_factory=from_env(
|
||||
"OPENROUTER_APP_URL",
|
||||
default="https://docs.langchain.com/oss",
|
||||
),
|
||||
)
|
||||
"""Application URL for OpenRouter attribution. Maps to `HTTP-Referer` header."""
|
||||
"""Application URL for OpenRouter attribution.
|
||||
|
||||
Maps to `HTTP-Referer` header.
|
||||
|
||||
See https://openrouter.ai/docs/app-attribution for details.
|
||||
"""
|
||||
|
||||
app_title: str | None = Field(
|
||||
default_factory=from_env("OPENROUTER_APP_TITLE", default=None),
|
||||
default_factory=from_env("OPENROUTER_APP_TITLE", default="langchain"),
|
||||
)
|
||||
"""Application title for OpenRouter attribution. Maps to `X-Title` header."""
|
||||
"""Application title for OpenRouter attribution.
|
||||
|
||||
Maps to `X-Title` header.
|
||||
|
||||
See https://openrouter.ai/docs/app-attribution for details.
|
||||
"""
|
||||
|
||||
request_timeout: int | None = Field(default=None, alias="timeout")
|
||||
"""Timeout for requests in milliseconds. Maps to SDK `timeout_ms`."""
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
'ChatOpenRouter',
|
||||
]),
|
||||
'kwargs': dict({
|
||||
'app_title': 'langchain',
|
||||
'app_url': 'https://docs.langchain.com/oss',
|
||||
'max_retries': 2,
|
||||
'max_tokens': 100,
|
||||
'model_name': 'openai/gpt-4o-mini',
|
||||
|
||||
@@ -310,6 +310,32 @@ class TestChatOpenRouterInstantiation:
|
||||
call_kwargs = mock_cls.call_args[1]
|
||||
assert call_kwargs["x_title"] == "My App"
|
||||
|
||||
def test_default_attribution_headers(self) -> None:
|
||||
"""Test that default attribution headers are sent when not overridden."""
|
||||
with patch("openrouter.OpenRouter") as mock_cls:
|
||||
mock_cls.return_value = MagicMock()
|
||||
ChatOpenRouter(
|
||||
model=MODEL_NAME,
|
||||
api_key=SecretStr("test-key"),
|
||||
)
|
||||
call_kwargs = mock_cls.call_args[1]
|
||||
assert call_kwargs["http_referer"] == ("https://docs.langchain.com/oss")
|
||||
assert call_kwargs["x_title"] == "langchain"
|
||||
|
||||
def test_user_attribution_overrides_defaults(self) -> None:
|
||||
"""Test that user-supplied attribution overrides the defaults."""
|
||||
with patch("openrouter.OpenRouter") as mock_cls:
|
||||
mock_cls.return_value = MagicMock()
|
||||
ChatOpenRouter(
|
||||
model=MODEL_NAME,
|
||||
api_key=SecretStr("test-key"),
|
||||
app_url="https://my-custom-app.com",
|
||||
app_title="My Custom App",
|
||||
)
|
||||
call_kwargs = mock_cls.call_args[1]
|
||||
assert call_kwargs["http_referer"] == "https://my-custom-app.com"
|
||||
assert call_kwargs["x_title"] == "My Custom App"
|
||||
|
||||
def test_reasoning_in_params(self) -> None:
|
||||
"""Test that `reasoning` is included in default params."""
|
||||
model = _make_model(reasoning={"effort": "high"})
|
||||
|
||||
2
libs/partners/openrouter/uv.lock
generated
2
libs/partners/openrouter/uv.lock
generated
@@ -318,7 +318,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langchain-core"
|
||||
version = "1.2.13"
|
||||
version = "1.2.15"
|
||||
source = { editable = "../../core" }
|
||||
dependencies = [
|
||||
{ name = "jsonpatch" },
|
||||
|
||||
Reference in New Issue
Block a user