langchain-ai/langchain · error · ValueError

{response.text}

Error message

{response.text}

What it means

Raised by `raise_for_status_with_text` in `langchain_core.utils.utils`: it wraps `requests.Response.raise_for_status()` and, on any `HTTPError`, re-raises as `ValueError(response.text)` so the provider's error body is visible in the exception message. Seeing this error means the HTTP call to the model/provider API returned a 4xx/5xx status; the message text is the server's response body.

Source

Thrown at libs/core/langchain_core/utils/utils.py:70

        return wrapper

    return decorator


def raise_for_status_with_text(response: Response) -> None:
    """Raise an error with the response text.

    Args:
        response: The response to check for errors.

    Raises:
        ValueError: If the response has an error status code.
    """
    try:
        response.raise_for_status()
    except HTTPError as e:
        raise ValueError(response.text) from e


@contextlib.contextmanager
def mock_now(dt_value: datetime.datetime) -> Iterator[type]:
    """Context manager for mocking out datetime.now() in unit tests.

    Args:
        dt_value: The datetime value to use for datetime.now().

    Yields:
        The mocked datetime class.

    Example:
        ```python
        with mock_now(datetime.datetime(2011, 2, 3, 10, 11)):
            assert datetime.datetime.now() == datetime.datetime(2011, 2, 3, 10, 11)
        ```
    """

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Read the message body — it is the provider's own error JSON and names the real cause (auth, quota, model name).
  2. Fix credentials/endpoints: verify the API key env var is set and the model name/base_url are correct for the provider.
  3. For 429/5xx add retry with exponential backoff and honor `Retry-After`.
  4. If the message is HTML, a proxy or captive portal intercepted the request — check network egress and proxy settings.

Example fix

# before
resp = requests.post(url, json=payload, headers=headers)
raise_for_status_with_text(resp)  # ValueError: {"error": {"message": "Invalid API key"}}
# fix: set the real key / endpoint before the call
os.environ["<PROVIDER>_API_KEY"] = "sk-..."  # correct key

# defensive caller-side handling
from requests.adapters import HTTPAdapter, Retry
session = requests.Session()
session.mount("https://", HTTPAdapter(max_retries=Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503])))
Defensive patterns

Strategy: retry

Validate before calling

def ok_to_send(response) -> bool:
    return response.status_code < 400

if not ok_to_send(resp):
    raise ValueError(resp.text)  # caller decides retry vs abort

Try / catch

import time
for attempt in range(5):
    resp = session.post(url, json=payload, timeout=30)
    if resp.status_code in (429, 500, 502, 503):
        time.sleep(2 ** attempt)
        continue
    try:
        raise_for_status_with_text(resp)
    except ValueError:
        # message body names the real cause: auth, quota, model name
        raise
    break

Prevention

When it happens

Trigger: An LLM/provider client (commonly in `langchain-classic` community integrations) receives a 401/403 (bad API key), 404 (wrong endpoint/model name), 429 (rate limit), or 500/503 (provider outage) and calls `raise_for_status_with_text(response)`; the resulting `ValueError` message contains the JSON error body from the provider.

Common situations: Missing/expired API key env var; hitting rate limits or quota; wrong base URL or model identifier; provider incidents; proxy/firewall returning an HTML error page (so the message looks like HTML).

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/20e6bdd39b3f1bb0. Report an issue: GitHub.