Home / File/ _client_utils.py — langchain Source File

_client_utils.py — langchain Source File

Architecture documentation for _client_utils.py, a python file in the langchain codebase. 5 imports, 0 dependents.

File python CoreAbstractions MessageSchema 5 imports 2 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  e0637884_0319_6473_9689_47a55cbaed0f["_client_utils.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  e0637884_0319_6473_9689_47a55cbaed0f --> a327e534_84f6_5308_58ca_5727d5eda0cf
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  e0637884_0319_6473_9689_47a55cbaed0f --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  e0637884_0319_6473_9689_47a55cbaed0f --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  e0637884_0319_6473_9689_47a55cbaed0f --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  ad604472_c022_b119_aeba_5ff8893361cd["anthropic"]
  e0637884_0319_6473_9689_47a55cbaed0f --> ad604472_c022_b119_aeba_5ff8893361cd
  style e0637884_0319_6473_9689_47a55cbaed0f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Helpers for creating Anthropic API clients.

This module allows for the caching of httpx clients to avoid creating new instances
for each instance of ChatAnthropic.

Logic is largely replicated from anthropic._base_client.
"""

from __future__ import annotations

import asyncio
import os
from functools import lru_cache
from typing import Any

import anthropic

_NOT_GIVEN: Any = object()


class _SyncHttpxClientWrapper(anthropic.DefaultHttpxClient):
    """Borrowed from anthropic._base_client."""

    def __del__(self) -> None:
        if self.is_closed:
            return

        try:
            self.close()
        except Exception:  # noqa: S110
            pass


class _AsyncHttpxClientWrapper(anthropic.DefaultAsyncHttpxClient):
    """Borrowed from anthropic._base_client."""

    def __del__(self) -> None:
        if self.is_closed:
            return

        try:
            # TODO(someday): support non asyncio runtimes here
            asyncio.get_running_loop().create_task(self.aclose())
        except Exception:  # noqa: S110
            pass


@lru_cache
def _get_default_httpx_client(
    *,
    base_url: str | None,
    timeout: Any = _NOT_GIVEN,
    anthropic_proxy: str | None = None,
) -> _SyncHttpxClientWrapper:
    kwargs: dict[str, Any] = {
        "base_url": base_url
        or os.environ.get("ANTHROPIC_BASE_URL")
        or "https://api.anthropic.com",
    }
    if timeout is not _NOT_GIVEN:
        kwargs["timeout"] = timeout
    if anthropic_proxy is not None:
        kwargs["proxy"] = anthropic_proxy
    return _SyncHttpxClientWrapper(**kwargs)


@lru_cache
def _get_default_async_httpx_client(
    *,
    base_url: str | None,
    timeout: Any = _NOT_GIVEN,
    anthropic_proxy: str | None = None,
) -> _AsyncHttpxClientWrapper:
    kwargs: dict[str, Any] = {
        "base_url": base_url
        or os.environ.get("ANTHROPIC_BASE_URL")
        or "https://api.anthropic.com",
    }
    if timeout is not _NOT_GIVEN:
        kwargs["timeout"] = timeout
    if anthropic_proxy is not None:
        kwargs["proxy"] = anthropic_proxy
    return _AsyncHttpxClientWrapper(**kwargs)

Subdomains

Dependencies

  • anthropic
  • asyncio
  • functools
  • os
  • typing

Frequently Asked Questions

What does _client_utils.py do?
_client_utils.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in _client_utils.py?
_client_utils.py defines 2 function(s): _get_default_async_httpx_client, _get_default_httpx_client.
What does _client_utils.py depend on?
_client_utils.py imports 5 module(s): anthropic, asyncio, functools, os, typing.
Where is _client_utils.py in the architecture?
_client_utils.py is located at libs/partners/anthropic/langchain_anthropic/_client_utils.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/partners/anthropic/langchain_anthropic).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free