langchain-ai/langgraph · error · ValueError
Cannot set reserved header
Error message
Cannot set reserved header '{header}' What it means
Header guard in _get_headers: the SDK reserves certain headers (e.g. User-Agent, x-api-key) which it sets itself from the api_key and version. A caller-supplied custom_headers entry colliding with a reserved name is rejected so it cannot override authentication metadata.
Solutions
- Remove the reserved header from your custom_headers dict
- Pass your API key via the api_key argument instead of setting x-api-key manually
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at libs/sdk-py/langgraph_sdk/_shared/utilities.py:59 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of langchain-ai/langgraph@38031739e5 (2026-08-26).
Data as JSON: /api/errors/393db5240c4d3723.
Report an issue: GitHub.
Appendix: source
Thrown at libs/sdk-py/langgraph_sdk/_shared/utilities.py:59
return api_key
if api_key is NOT_PROVIDED:
# api_key is not explicitly provided, try to load from environment
for prefix in ["LANGGRAPH", "LANGSMITH", "LANGCHAIN"]:
if env := os.getenv(f"{prefix}_API_KEY"):
return env.strip().strip('"').strip("'")
# api_key is explicitly None, don't load from environment
return None
def _get_headers(
api_key: str | None,
custom_headers: Mapping[str, str] | None,
) -> dict[str, str]:
"""Combine api_key and custom user-provided headers."""
custom_headers = custom_headers or {}
for header in RESERVED_HEADERS:
if header in custom_headers:
raise ValueError(f"Cannot set reserved header '{header}'")
headers = {
"User-Agent": f"langgraph-sdk-py/{langgraph_sdk.__version__}",
**custom_headers,
}
resolved_api_key = _get_api_key(api_key)
if resolved_api_key:
headers["x-api-key"] = resolved_api_key
return headers
def _orjson_default(obj: Any) -> Any:
is_class = isinstance(obj, type)
if hasattr(obj, "model_dump") and callable(obj.model_dump):
if is_class:
raise TypeError(
f"Cannot JSON-serialize type object: {obj!r}. Did you mean to pass an instance of the object instead?"View on GitHub (pinned to 38031739e5)