Skip to content

Conversation

NirAyalon1
Copy link

@NirAyalon1 NirAyalon1 commented Sep 24, 2025

Introduction

The OpenAI Python SDK now supports an optional aiohttp backend that removes concurrency limits seen with HTTPX. This PR exposes an opt-in path in LangChain's OpenAI integration to allow http_client=DefaultAioHttpClient() or an environment variable toggle.

Based on this issue

What changed

  • New optional kwarg: Enhanced existing http_client and http_async_client parameters to support DefaultAioHttpClient
  • Environment variable: Added LC_OPENAI_USE_AIOHTTP environment variable for convenient opt-in
  • Graceful fallback: Warns when env var is set but openai[aiohttp] is not installed
  • No default behavior changed: All changes are optional and backward compatible
  • Unit tests: Comprehensive test coverage for all scenarios
  • Documentation: Updated README with installation and usage examples

Why

Improve throughput for parallel chains, graphs, and agents without breaking existing users. The default HTTPX async client in OpenAI has concurrency limitations that can be removed by using the aiohttp backend.

How to enable

Option 1: Install and set environment variable

pip install "openai[aiohttp]"
# Set LC_OPENAI_USE_AIOHTTP=1 in your environment
from langchain_openai import ChatOpenAI

llm = ChatOpenAI()  # Will automatically use aiohttp if available

Option 2: Pass explicit client

pip install "openai[aiohttp]"
from openai import DefaultAioHttpClient
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    http_client=DefaultAioHttpClient(),
    http_async_client=DefaultAioHttpClient()
)

Backwards compatibility

No breaking changes - this is an optional parameter only
Existing behavior preserved - default httpx clients still used by default
Graceful degradation - warns and falls back if aiohttp not available
Proper precedence - explicit client parameters override environment variables

Testing

  • Unit tests for explicit client passing
  • Unit tests for environment variable enabling/disabling
  • Unit tests for fallback behavior with warnings
  • Unit tests for truthy environment variable values
  • Unit tests for precedence (explicit overrides env var)
  • No network calls in tests (mocked OpenAI constructors)
  • All existing tests still pass
  • Linting and formatting checks pass

References

Code Quality Checklist

  • Breaking Changes: ✅ No public API changes
  • Type Hints: ✅ All functions have complete type annotations
  • Tests: ✅ New functionality is fully tested with comprehensive coverage
  • Security: ✅ No dangerous patterns (proper import handling, graceful fallbacks)
  • Documentation: ✅ Google-style docstrings and README updates
  • Code Quality: ✅ make lint and make format pass
  • Architecture: ✅ Clean separation of concerns, backward compatible design
  • Commit Message: ✅ Follows Conventional Commits format

Ready for review 🚀

Copy link

vercel bot commented Sep 24, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain Ignored Ignored Preview Sep 24, 2025 8:37pm

@github-actions github-actions bot added documentation Improvements or additions to documentation integration Related to a provider partner package integration labels Sep 24, 2025
Copy link

codspeed-hq bot commented Sep 24, 2025

CodSpeed Instrumentation Performance Report

Merging #33084 will not alter performance

Comparing NirAyalon1:feat/openai-aiohttp-backend (0866607) with master (b92b394)1

Summary

✅ 6 untouched
⏩ 11 skipped2

Footnotes

  1. No successful run was found on master (50012d9) during the generation of this report, so b92b394 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 11 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@NirAyalon1
Copy link
Author

Note: The PR title labeler workflow is failing due to permissions restrictions for fork PRs. The workflow should use pull_request_target trigger instead of pull_request to work properly with forks.

@github-actions github-actions bot added feature and removed documentation Improvements or additions to documentation integration Related to a provider partner package integration labels Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants