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

This commit is contained in:
Matt Van Horn
2026-03-11 20:07:45 -07:00
committed by GitHub
parent 92be5b62b0
commit 9521c679db

View File

@@ -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