langchain-ai/langchain

AzureChatOpenAI creates a new `httpx` client on each instantiation

Open

#32,489 opened on Aug 10, 2025

View on GitHub
 (16 comments) (0 reactions) (1 assignee)Python (22,617 forks)batch import
bugexternalhelp wantedopenai

Repository metrics

Stars
 (136,758 stars)
PR merge metrics
 (Avg merge 10d 2h) (288 merged PRs in 30d)

Description

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • 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 (or the specific integration package).
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Example Code

Code Example

import asyncio
import httpx
from langchain_openai import AzureChatOpenAI

# Track httpx client creations
original_init = httpx.AsyncClient.__init__
counter = {"count": 0}

def counting_init(self, *args, **kwargs):
    counter["count"] += 1
    print(f"New AsyncClient created: {counter['count']}")
    original_init(self, *args, **kwargs)

httpx.AsyncClient.__init__ = counting_init

def create_llm():
    return AzureChatOpenAI(
        azure_endpoint="https://<REDACTED-ENDPOINT>/",
        deployment_name="<REDACTED-DEPLOYMENT-NAME>",
        temperature=0.7
    )

async def main():
    llms = [create_llm() for _ in range(5)]
    print(f"Total AsyncClient instances: {counter['count']}")

asyncio.run(main())

Error Message and Stack Trace (if applicable)

No response

Description

langchain-core==0.3.65 langchain-openai==0.3.23

System Info

backports.tarfile==1.2.0 certifi==2025.8.3 charset-normalizer==3.4.2 docutils==0.22 id==1.5.0 idna==3.10 importlib_metadata==8.7.0 jaraco.classes==3.4.0 jaraco.context==6.0.1 jaraco.functools==4.2.1 keyring==25.6.0 markdown-it-py==3.0.0 mdurl==0.1.2 more-itertools==10.7.0 nh3==0.3.0 packaging==25.0 Pygments==2.19.2 readme_renderer==44.0 requests==2.32.4 requests-toolbelt==1.0.0 rfc3986==2.0.0 rich==14.1.0 twine==6.1.0 urllib3==2.5.0 zipp==3.23.0

Contributor guide