mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-17 18:45:44 +00:00
fix(openai): close PIL Image handles in token counting to prevent fd leak (#35742)
This commit is contained in:
@@ -3693,8 +3693,8 @@ def _url_to_size(image_source: str) -> tuple[int, int] | None:
|
||||
)
|
||||
return None
|
||||
|
||||
# close things (context managers)
|
||||
width, height = Image.open(BytesIO(response.content)).size
|
||||
with Image.open(BytesIO(response.content)) as img:
|
||||
width, height = img.size
|
||||
return width, height
|
||||
except httpx.TimeoutException:
|
||||
logger.warning("Image URL request timed out after %s seconds", timeout)
|
||||
@@ -3709,7 +3709,8 @@ def _url_to_size(image_source: str) -> tuple[int, int] | None:
|
||||
if _is_b64(image_source):
|
||||
_, encoded = image_source.split(",", 1)
|
||||
data = base64.b64decode(encoded)
|
||||
width, height = Image.open(BytesIO(data)).size
|
||||
with Image.open(BytesIO(data)) as img:
|
||||
width, height = img.size
|
||||
return width, height
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user