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. 8 imports, 0 dependents.

File python CoreAbstractions MessageSchema 8 imports 7 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  b5e830e7_ec0e_2621_0651_5ceaab19651f["_client_utils.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> a327e534_84f6_5308_58ca_5727d5eda0cf
  614e7b9f_ed51_0780_749c_ff40b74963fc["inspect"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> 614e7b9f_ed51_0780_749c_ff40b74963fc
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  45fc8fd3_e815_b442_a6d1_0dedc9327b62["openai"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> 45fc8fd3_e815_b442_a6d1_0dedc9327b62
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  b5e830e7_ec0e_2621_0651_5ceaab19651f --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  style b5e830e7_ec0e_2621_0651_5ceaab19651f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Helpers for creating OpenAI API clients.

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

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

from __future__ import annotations

import asyncio
import inspect
import os
from collections.abc import Awaitable, Callable
from functools import lru_cache
from typing import Any, cast

import openai
from pydantic import SecretStr


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

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

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


class _AsyncHttpxClientWrapper(openai.DefaultAsyncHttpxClient):
    """Borrowed from openai._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


def _build_sync_httpx_client(
    base_url: str | None, timeout: Any
) -> _SyncHttpxClientWrapper:
    return _SyncHttpxClientWrapper(
        base_url=base_url
        or os.environ.get("OPENAI_BASE_URL")
        or "https://api.openai.com/v1",
        timeout=timeout,
    )


def _build_async_httpx_client(
// ... (83 more lines)

Subdomains

Dependencies

  • asyncio
  • collections.abc
  • functools
  • inspect
  • openai
  • os
  • pydantic
  • 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 7 function(s): _build_async_httpx_client, _build_sync_httpx_client, _cached_async_httpx_client, _cached_sync_httpx_client, _get_default_async_httpx_client, _get_default_httpx_client, _resolve_sync_and_async_api_keys.
What does _client_utils.py depend on?
_client_utils.py imports 8 module(s): asyncio, collections.abc, functools, inspect, openai, os, pydantic, typing.
Where is _client_utils.py in the architecture?
_client_utils.py is located at libs/partners/openai/langchain_openai/chat_models/_client_utils.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/partners/openai/langchain_openai/chat_models).

Analyze Your Own Codebase

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

Try Supermodel Free