langchain - ✅(Solved) Fix openai: Image.open() file descriptor leak in token counting [3 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35728Fetched 2026-04-08 00:24:50
View on GitHub
Comments
3
Participants
3
Timeline
11
Reactions
0
Assignees
Timeline (top)
commented ×3cross-referenced ×3referenced ×2assigned ×1

Fix Action

Fixed

PR fix notes

PR #35731: fix(openai): use context manager for Image.open to prevent resource leak

Description (problem / solution / changelog)

Fixes #35728

Convert Image.open() calls to use with context manager in token counting code. File descriptors leak if Image.open() is not properly closed.

Changed file

  • libs/partners/openai/langchain_openai/chat_models/base.py

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +4/-2)

PR #35742: fix(openai): close PIL Image handles in token counting to prevent fd leak

Description (problem / solution / changelog)

Summary

  • Wraps two Image.open() calls in _get_image_dimensions() with context managers (with statements) to ensure PIL Image handles are properly closed and file descriptors released.
  • The first call (line ~3697) even had a comment # close things (context managers) that was never implemented.
  • The second call (line ~3712) for base64-encoded images had the same leak.

Fixes #35728

Test plan

  • Verify existing unit tests pass for token counting
  • No new tests needed - this is a resource cleanup fix with identical behavior

This PR was developed with AI agent assistance.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +4/-3)

PR #35768: fix(openai): use context managers for Image.open() to prevent file descriptor leaks

Description (problem / solution / changelog)

Fixes #35728

Wrapped Image.open() calls with context managers to ensure file descriptors are properly cleaned up. This prevents resource leaks when processing images for token counting in vision models.

Changes

  • Line 3697: Added with context manager for URL image processing
  • Line 3712: Added with context manager for base64 image processing

This addresses the comment on line 3696 that said "close things (context managers)" but was not followed.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +4/-2)
RAW_BUFFERClick to expand / collapse

Description

Image.open() calls in the OpenAI token counting code don't use context managers, causing file descriptor leaks. There's even a comment on line 3696 about "close things (context managers)" that isn't followed.

Affected file

  • libs/partners/openai/langchain_openai/chat_models/base.py — token counting code

Suggested fix Wrap Image.open() calls with with context manager to ensure file descriptors are properly cleaned up.

extent analysis

Fix: File Descriptor Leak in OpenAI Token Counting Code

Problem Summary

File descriptor leaks occur due to missing context managers in Image.open() calls.

Fix Plan

  1. Identify Image.open() calls: In the libs/partners/openai/langchain_openai/chat_models/base.py file, locate all instances of Image.open().
  2. Wrap with context manager: Replace each Image.open() call with a with statement to ensure file descriptors are properly cleaned up.

Example Code

# Before (leak-prone)
img = Image.open('image.jpg')

# After (safe with context manager)
with Image.open('image.jpg') as img:
    # Process the image
    pass
  1. Review and update comments: Remove the comment on line 3696 about "close things (context manager)" since the issue is now resolved.

Verification

  1. Run the code with the updated Image.open() calls.
  2. Monitor the system for file descriptor leaks.
  3. Verify that the file descriptors are properly cleaned up after each Image.open() call.

Extra Tips

  • Always use context managers when working with file descriptors or other resources to prevent leaks.
  • Regularly review code for potential issues like this one to maintain a clean and efficient system.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - ✅(Solved) Fix openai: Image.open() file descriptor leak in token counting [3 pull requests, 3 comments, 3 participants]