Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions libs/partners/xai/langchain_xai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import openai
from langchain_core.messages import AIMessageChunk
from langchain_core.utils import from_env, secret_from_env
from langchain_core.utils._gateway import _apply_gateway_config
from langchain_core.utils._gateway import (
_apply_gateway_config,
_gateway_headers,
_langsmith_gateway_active,
_with_gateway_headers,
)
from langchain_openai.chat_models.base import BaseChatOpenAI
from pydantic import BaseModel, ConfigDict, Field, SecretStr, model_validator

Expand Down Expand Up @@ -535,6 +540,8 @@ def _apply_xai_gateway_config(cls, values: Any) -> Any:
@model_validator(mode="after")
def validate_environment(self) -> Self:
"""Validate that api key and python package exists in environment."""
if _langsmith_gateway_active():
self._langsmith_gateway_headers = _gateway_headers()
if self.n is not None and self.n < 1:
msg = "n must be at least 1."
raise ValueError(msg)
Expand All @@ -548,7 +555,9 @@ def validate_environment(self) -> Self:
),
"base_url": self.xai_api_base,
"timeout": self.request_timeout,
"default_headers": self.default_headers,
"default_headers": _with_gateway_headers(
self.default_headers, self._langsmith_gateway_headers
),
"default_query": self.default_query,
}
if self.max_retries is not None:
Expand Down
16 changes: 16 additions & 0 deletions libs/partners/xai/tests/unit_tests/test_chat_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import pytest # type: ignore[import-not-found]
from langchain_core.load import dumpd
from langchain_core.messages import (
AIMessage,
AIMessageChunk,
Expand All @@ -9,6 +10,7 @@
SystemMessage,
ToolMessage,
)
from langchain_core.utils._gateway import langsmith_gateway
from langchain_openai.chat_models.base import (
_convert_dict_to_message,
_convert_message_to_dict,
Expand Down Expand Up @@ -432,3 +434,17 @@ def test_langsmith_gateway_context_overrides_explicit_values() -> None:
assert llm.xai_api_base == "https://gateway.smith.langchain.com/xai/v1"
assert llm.xai_api_key is not None
assert llm.xai_api_key.get_secret_value() == "service-key"


def test_langsmith_gateway_context_headers() -> None:
"""Gateway headers reach the client but are never serialized."""
with langsmith_gateway(api_key="placeholder", headers={"X-Service-Key": "svc-key"}):
llm = ChatXAI(
model="grok-4",
api_key=SecretStr("user-key"),
default_headers={"x-service-key": "forged", "X-Custom": "kept"},
)
sent = llm.root_client.default_headers
assert sent["X-Service-Key"] == "svc-key"
assert sent["X-Custom"] == "kept"
assert "svc-key" not in json.dumps(dumpd(llm))
Loading