Skip to content

fix: prevent LangChain connection errors from garbage collection#73

Merged
lfnovo merged 2 commits intomainfrom
fix/langchain-http-client-gc-issue
Jan 24, 2026
Merged

fix: prevent LangChain connection errors from garbage collection#73
lfnovo merged 2 commits intomainfrom
fix/langchain-http-client-gc-issue

Conversation

@lfnovo
Copy link
Owner

@lfnovo lfnovo commented Jan 24, 2026

Summary

  • Fixed a bug where LangChain models would get "Connection error" after the Esperanto model was garbage collected
  • Added _create_langchain_http_clients() method to create fresh httpx clients for LangChain
  • Updated 5 affected providers to use the new method

Problem

When to_langchain() was called, Esperanto shared its httpx clients with LangChain. When the Esperanto model went out of scope and was garbage collected:

  1. HttpConnectionMixin.__del__() was called
  2. This closed the shared httpx clients
  3. LangChain still held references to these now-closed clients
  4. When LangChain tried to use them → "Connection error"

Solution

Instead of sharing Esperanto's HTTP clients with LangChain, we now create fresh httpx clients with the same configuration (timeout, SSL, proxy). This gives LangChain ownership of its own clients that won't be affected when the Esperanto model is garbage collected.

Files Changed

  • src/esperanto/utils/connect.py - Added _create_langchain_http_clients() method
  • src/esperanto/providers/llm/openai.py - Use new method
  • src/esperanto/providers/llm/groq.py - Use new method
  • src/esperanto/providers/llm/perplexity.py - Use new method
  • src/esperanto/providers/llm/openai_compatible.py - Use new method
  • src/esperanto/providers/llm/azure.py - Use new method

Test plan

  • All 390 existing tests pass
  • All 33 LangChain-related tests pass

Fixes: lfnovo/open-notebook#460

When to_langchain() was called, Esperanto shared its httpx clients with
LangChain. When the Esperanto model went out of scope and was garbage
collected, __del__ closed those shared clients, causing LangChain to
get "Connection error" when trying to make requests.

This fix adds _create_langchain_http_clients() method that creates fresh
httpx clients with the same configuration (timeout, SSL, proxy). This
gives LangChain ownership of its own clients that won't be affected
when the Esperanto model is garbage collected.

Affected providers: openai, groq, perplexity, openai_compatible, azure

Fixes: lfnovo/open-notebook#460
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

@claude
Copy link

claude bot commented Jan 24, 2026


Code review

No issues found. Checked for bugs and CLAUDE.md compliance.


@claude
Copy link

claude bot commented Jan 24, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@lfnovo lfnovo merged commit 431fc43 into main Jan 24, 2026
6 checks passed
@lfnovo lfnovo deleted the fix/langchain-http-client-gc-issue branch January 24, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Install]: Error in source chat streaming: Connection error.

1 participant