langchain - 💡(How to fix) Fix openai: AzureChatOpenAI should reuse cached default httpx clients [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

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#36109Fetched 2026-04-08 01:03:31
View on GitHub
Comments
2
Participants
2
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
commented ×2labeled ×2closed ×1

AzureChatOpenAI does not reuse the cached default httpx clients that ChatOpenAI already uses.

In langchain_openai.chat_models.base.BaseChatOpenAI.validate_environment(), the default path uses cached helper functions for sync and async httpx clients.

However, langchain_openai.chat_models.azure.AzureChatOpenAI.validate_environment() currently passes only the explicitly supplied http_client / http_async_client. When those are unset, the OpenAI SDK creates fresh clients internally for each instance.

This creates unnecessary client churn when applications instantiate many AzureChatOpenAI objects.

Root Cause

AzureChatOpenAI does not reuse the cached default httpx clients that ChatOpenAI already uses.

In langchain_openai.chat_models.base.BaseChatOpenAI.validate_environment(), the default path uses cached helper functions for sync and async httpx clients.

However, langchain_openai.chat_models.azure.AzureChatOpenAI.validate_environment() currently passes only the explicitly supplied http_client / http_async_client. When those are unset, the OpenAI SDK creates fresh clients internally for each instance.

This creates unnecessary client churn when applications instantiate many AzureChatOpenAI objects.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find similar issues.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain.
  • I posted a self-contained, minimal, reproducible example.

Code Example

import httpx
from langchain_openai import AzureChatOpenAI

original_sync_init = httpx.Client.__init__
original_async_init = httpx.AsyncClient.__init__

counter = {"sync": 0, "async": 0}

def counting_sync_init(self, *args, **kwargs):
    counter["sync"] += 1
    return original_sync_init(self, *args, **kwargs)

def counting_async_init(self, *args, **kwargs):
    counter["async"] += 1
    return original_async_init(self, *args, **kwargs)

httpx.Client.__init__ = counting_sync_init
httpx.AsyncClient.__init__ = counting_async_init

for _ in range(5):
    AzureChatOpenAI(
        api_key="test",
        azure_endpoint="https://example.openai.azure.com/",
        azure_deployment="my-deployment",
        openai_api_version="2023-05-15",
    )

print(counter)
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find similar issues.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain.
  • I posted a self-contained, minimal, reproducible example.

Package (Required)

  • langchain-openai

Description

AzureChatOpenAI does not reuse the cached default httpx clients that ChatOpenAI already uses.

In langchain_openai.chat_models.base.BaseChatOpenAI.validate_environment(), the default path uses cached helper functions for sync and async httpx clients.

However, langchain_openai.chat_models.azure.AzureChatOpenAI.validate_environment() currently passes only the explicitly supplied http_client / http_async_client. When those are unset, the OpenAI SDK creates fresh clients internally for each instance.

This creates unnecessary client churn when applications instantiate many AzureChatOpenAI objects.

Minimal Reproducible Example

import httpx
from langchain_openai import AzureChatOpenAI

original_sync_init = httpx.Client.__init__
original_async_init = httpx.AsyncClient.__init__

counter = {"sync": 0, "async": 0}

def counting_sync_init(self, *args, **kwargs):
    counter["sync"] += 1
    return original_sync_init(self, *args, **kwargs)

def counting_async_init(self, *args, **kwargs):
    counter["async"] += 1
    return original_async_init(self, *args, **kwargs)

httpx.Client.__init__ = counting_sync_init
httpx.AsyncClient.__init__ = counting_async_init

for _ in range(5):
    AzureChatOpenAI(
        api_key="test",
        azure_endpoint="https://example.openai.azure.com/",
        azure_deployment="my-deployment",
        openai_api_version="2023-05-15",
    )

print(counter)

Expected Behavior

Repeated AzureChatOpenAI(...) construction should reuse cached default sync/async httpx clients, matching ChatOpenAI behavior.

Actual Behavior

A new underlying client is created per instance when custom http_client / http_async_client are not supplied.

Proposed Fix

Use the same cached default client helpers in azure.py that BaseChatOpenAI already uses for the non-Azure OpenAI chat model.

Additional Context

I already have a local fix and regression test prepared for this.

extent analysis

Fix Plan

To fix the issue, we need to modify the AzureChatOpenAI class to reuse the cached default httpx clients. We can achieve this by using the same cached default client helpers in azure.py that BaseChatOpenAI already uses.

Here are the steps:

  • Modify the validate_environment method in AzureChatOpenAI to use the cached default client helpers.
  • Remove the explicit creation of new httpx clients when http_client and http_async_client are not supplied.

Example code:

from langchain_openai.chat_models.base import get_default_sync_client, get_default_async_client

class AzureChatOpenAI:
    # ...

    def validate_environment(self):
        # ...
        if self.http_client is None:
            self.http_client = get_default_sync_client()
        if self.http_async_client is None:
            self.http_async_client = get_default_async_client()
        # ...

By using the get_default_sync_client and get_default_async_client functions, we ensure that the cached default clients are reused, reducing unnecessary client churn.

Verification

To verify that the fix worked, you can run the minimal reproducible example provided in the issue body. The counter dictionary should show that the same cached clients are being reused for each instance of AzureChatOpenAI.

print(counter)  # Should print {'sync': 1, 'async': 1}

This indicates that the fix is working as expected, and the cached default clients are being reused.

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

langchain - 💡(How to fix) Fix openai: AzureChatOpenAI should reuse cached default httpx clients [2 comments, 2 participants]