Skip to content

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

Merged
ccurme (ccurme) merged 4 commits into
langchain-ai:masterfrom
mvanhorn:fix/image-fd-leak
Mar 12, 2026
Merged

fix(openai): close PIL Image handles in token counting to prevent fd leak#35742
ccurme (ccurme) merged 4 commits into
langchain-ai:masterfrom
mvanhorn:fix/image-fd-leak

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

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.

…leak

Wrap Image.open() calls in context managers to ensure file descriptors
are properly released after reading image dimensions. Addresses the
existing "close things (context managers)" comment that was not followed.

Fixes langchain-ai#35728

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions Bot added size: XS < 50 LOC integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs fix For PRs that implement a fix and removed size: XS < 50 LOC labels Mar 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically closed because you are not assigned to the linked issue.

External contributors must be assigned to an issue before opening a PR for it. Please:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, edit your PR description and the PR will be reopened automatically

@github-actions github-actions Bot closed this Mar 11, 2026
@codspeed-hq

codspeed-hq Bot commented Mar 11, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 6 untouched benchmarks
⏩ 30 skipped benchmarks1


Comparing mvanhorn:fix/image-fd-leak (85d4929) with master (f9dbd22)2

Open in CodSpeed

Footnotes

  1. 30 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.

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

@github-actions github-actions Bot added the anthropic `langchain-anthropic` package issues & PRs label Mar 12, 2026
@ccurme
ccurme (ccurme) merged commit 9521c67 into langchain-ai:master Mar 12, 2026
154 of 157 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anthropic `langchain-anthropic` package issues & PRs external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openai: Image.open() file descriptor leak in token counting

2 participants