langchain - ✅(Solved) Fix feat(anthropic): add http_client and http_async_client fields to ChatAnthropic (parity with ChatOpenAI) [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36056Fetched 2026-04-08 00:52:54
View on GitHub
Comments
3
Participants
3
Timeline
9
Reactions
0
Timeline (top)
commented ×3labeled ×2cross-referenced ×1issue_type_added ×1

Fix Action

Fixed

PR fix notes

PR #36118: feat(anthropic): add http_client and http_async_client fields to ChatAnthropic

Description (problem / solution / changelog)

Description

Adds http_client and http_async_client fields to ChatAnthropic, matching the pattern already established in ChatOpenAI. This lets users inject custom httpx.Client/httpx.AsyncClient instances for mTLS, cert pinning, proxy tunneling, or test mocking.

When the fields are None (default), behavior is identical to the current implementation - a default httpx client is created via _get_default_httpx_client().

Fixes #36056

Changes

  • Added http_client: Any | None and http_async_client: Any | None fields with Field(default=None, exclude=True)
  • Modified _client cached property to use injected http_client when provided, skipping default client construction
  • Modified _async_client cached property to use injected http_async_client when provided
  • Added unit tests verifying injection passthrough and default behavior preservation

Testing

cd libs/partners/anthropic && make format lint test

All 81 unit tests pass (3 new). No changes to uv.lock or pyproject.toml.

This contribution was developed with AI assistance (Claude Code + Codex).

Changed files

  • libs/partners/anthropic/langchain_anthropic/chat_models.py (modified, +32/-12)
  • libs/partners/anthropic/tests/unit_tests/test_chat_models.py (modified, +29/-0)

Code Example

import httpx
from langchain_anthropic import ChatAnthropic

# Custom SSL CA bundle
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify="/etc/ssl/certs/my-ca.crt"),
    http_async_client=httpx.AsyncClient(verify="/etc/ssl/certs/my-ca.crt"),
)

# Disable SSL verification
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify=False),
    http_async_client=httpx.AsyncClient(verify=False),
)

---

http_client: Any | None = Field(default=None, exclude=True)
"""Optional `httpx.Client` for sync invocations."""
http_async_client: Any | None = Field(default=None, exclude=True)
"""Optional `httpx.AsyncClient` for async invocations."""
In the _client cached property, use the user-supplied client if provided, otherwise fall back to the existing _get_default_httpx_client():
@cached_property
def _client(self) -> anthropic.Anthropic:
    http_client = self.http_client or _get_default_httpx_client(**http_client_params)
    return anthropic.Anthropic(**client_params, http_client=http_client)
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

ChatOpenAI already supports passing a custom httpx.Client and httpx.AsyncClient directly via http_client and http_async_client fields (lines 763–775 in chat_models/base.py). This is used in practice for SSL customization, custom proxies, and corporate network environments.

ChatAnthropic has no equivalent. It always builds its own httpx client internally via _get_default_httpx_client(), and there is no way to inject a pre-configured one. The Anthropic SDK itself does support http_client as a constructor argument — ChatAnthropic just never exposes it.

Use Case

  • Disable SSL verification in dev/test environments: httpx.Client(verify=False)
  • Use a corporate CA bundle: httpx.Client(verify="/etc/ssl/certs/corporate-ca.crt")
  • Custom transport layers, timeouts per-request, or mTLS certificates
  • Environments where global SSL settings cannot be changed

Proposed Solution


import httpx
from langchain_anthropic import ChatAnthropic

# Custom SSL CA bundle
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify="/etc/ssl/certs/my-ca.crt"),
    http_async_client=httpx.AsyncClient(verify="/etc/ssl/certs/my-ca.crt"),
)

# Disable SSL verification
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify=False),
    http_async_client=httpx.AsyncClient(verify=False),
)

What needs to change in the code:

Add two new fields to ChatAnthropic in chat_models.py (same pattern as ChatOpenAI):

http_client: Any | None = Field(default=None, exclude=True)
"""Optional `httpx.Client` for sync invocations."""
http_async_client: Any | None = Field(default=None, exclude=True)
"""Optional `httpx.AsyncClient` for async invocations."""
In the _client cached property, use the user-supplied client if provided, otherwise fall back to the existing _get_default_httpx_client():
@cached_property
def _client(self) -> anthropic.Anthropic:
    http_client = self.http_client or _get_default_httpx_client(**http_client_params)
    return anthropic.Anthropic(**client_params, http_client=http_client)

Same for _async_client with http_async_client.

Alternatives Considered

Users have to subclass ChatAnthropic and override the _client and _async_client cached properties, which is fragile and not documented anywhere.

Additional Context

Related: #35977

Please assign this so I can implement the above plan.

extent analysis

Fix Plan

To add custom HTTP client support to ChatAnthropic, follow these steps:

  • Add two new fields to ChatAnthropic in chat_models.py:
    • http_client: an optional httpx.Client for sync invocations
    • http_async_client: an optional httpx.AsyncClient for async invocations
  • Update the _client cached property to use the user-supplied client if provided, otherwise fall back to the existing _get_default_httpx_client()
  • Update the _async_client cached property to use the user-supplied async client if provided

Example code changes:

from pydantic import Field
import httpx
from langchain_anthropic import ChatAnthropic

class ChatAnthropic:
    # ...
    http_client: Any | None = Field(default=None, exclude=True)
    http_async_client: Any | None = Field(default=None, exclude=True)

    @cached_property
    def _client(self) -> anthropic.Anthropic:
        http_client = self.http_client or _get_default_httpx_client(**http_client_params)
        return anthropic.Anthropic(**client_params, http_client=http_client)

    @cached_property
    def _async_client(self) -> anthropic.Anthropic:
        http_async_client = self.http_async_client or _get_default_httpx_client(**http_client_params)
        return anthropic.Anthropic(**client_params, http_client=http_async_client)

Verification

To verify the fix, create a ChatAnthropic instance with a custom http_client or http_async_client and check that it is used correctly. For example:

import httpx

# Custom SSL CA bundle
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify="/etc/ssl/certs/my-ca.crt"),
    http_async_client=httpx.AsyncClient(verify="/etc/ssl/certs/my-ca.crt"),
)

# Disable SSL verification
llm = ChatAnthropic(
    model="claude-opus-4-5",
    http_client=httpx.Client(verify=False),
    http_async_client=httpx.AsyncClient(verify=False),
)

Extra Tips

  • Make sure to test the fix with different custom HTTP client configurations to ensure it works as expected.
  • Consider adding documentation to ChatAnthropic to explain how to use the http_client and http_async_client fields.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING