From 656377bb3df4ccdc61f8a43ccc1e362cd7d8ddd1 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 13 May 2026 00:32:23 +0000 Subject: [PATCH] test(image_edits): regenerate fixtures per call instead of holding open module-level file handles Module-level TEST_IMAGES = [ open(os.path.join(pwd, 'ishaan_github.png'), 'rb'), open(os.path.join(pwd, 'litellm_site.png'), 'rb'), ] SINGLE_TEST_IMAGE = open(...) opens the file once at import. After the first multipart upload, the file pointer is at EOF, so every subsequent test in the same xdist worker sends an empty multipart body. That non-determinism (a) blows the recorded cassette past MAX_EPISODES_PER_CASSETTE (50) so _RedisPersister.save_cassette refuses to save it, and (b) re-bills the live image edit endpoint on every CI run. Recent CI runs confirm the leak: tests/image_gen_tests/test_image_edits.py shows six tests parking at 51-52 cassette entries (TestOpenAIImageEditGPTImage1::test_openai_image_edit_litellm_sdk[False], TestOpenAIImageEditDallE2::..., test_openai_image_edit_with_bytesio, test_openai_image_edit_litellm_router, test_multiple_vs_single_image_edit[False], test_multiple_image_edit_with_different_formats). Replace the module-level file handles with _make_test_images() / _make_single_test_image() factories that return fresh _RewindableImage (BytesIO subclass) objects whose pointer always starts at 0. The image bytes are read once at import into module-level constants (_ISHAAN_GITHUB_BYTES, _LITELLM_SITE_BYTES), so disk I/O cost is unchanged. Co-authored-by: Mateo Wang --- tests/image_gen_tests/test_image_edits.py | 86 ++++++++++++++++------- 1 file changed, 60 insertions(+), 26 deletions(-) diff --git a/tests/image_gen_tests/test_image_edits.py b/tests/image_gen_tests/test_image_edits.py index 6900bacdb9..441511b014 100644 --- a/tests/image_gen_tests/test_image_edits.py +++ b/tests/image_gen_tests/test_image_edits.py @@ -102,22 +102,57 @@ class BaseLLMImageEditTest(ABC): # Get the current directory of the file being run pwd = os.path.dirname(os.path.realpath(__file__)) -TEST_IMAGES = [ - open(os.path.join(pwd, "ishaan_github.png"), "rb"), - open(os.path.join(pwd, "litellm_site.png"), "rb"), -] -SINGLE_TEST_IMAGE = open(os.path.join(pwd, "ishaan_github.png"), "rb") +# Image fixtures must be regenerated per access — module-level +# ``open(...)`` handles get consumed after a single multipart upload, leaving +# subsequent tests in the same process to send empty bodies. That non-determinism +# (a) blows the recorded cassette past ``MAX_EPISODES_PER_CASSETTE`` so the +# persister refuses to save (see ``tests/_vcr_redis_persister.py``), and +# (b) re-bills the live image edit endpoint on every CI run. +def _read_image_bytes(filename: str) -> bytes: + with open(os.path.join(pwd, filename), "rb") as f: + return f.read() + + +_ISHAAN_GITHUB_BYTES = _read_image_bytes("ishaan_github.png") +_LITELLM_SITE_BYTES = _read_image_bytes("litellm_site.png") + + +class _RewindableImage(BytesIO): + """``BytesIO`` that re-seeks to 0 on read after exhaustion. + + The OpenAI / Azure SDKs read the file pointer once per request. When we + pass the *same* object to a parametrized or retried test, the second + invocation must see the same bytes from offset 0, not EOF. + """ + + def read(self, size=-1): + if self.tell() and self.tell() >= len(self.getvalue()): + self.seek(0) + return super().read(size) + + +def _make_test_images() -> list: + """Return a fresh pair of rewindable image streams. + + Use this everywhere you'd previously have used the module-level + ``TEST_IMAGES``. Each call returns brand new ``BytesIO`` objects whose + file pointers start at 0, so multipart uploads encode the full image + bytes on every test invocation. + """ + return [ + _RewindableImage(_ISHAAN_GITHUB_BYTES), + _RewindableImage(_LITELLM_SITE_BYTES), + ] + + +def _make_single_test_image() -> BytesIO: + return _RewindableImage(_ISHAAN_GITHUB_BYTES) def get_test_images_as_bytesio(): """Helper function to get test images as BytesIO objects""" - bytesio_images = [] - for image_path in ["ishaan_github.png", "litellm_site.png"]: - with open(os.path.join(pwd, image_path), "rb") as f: - image_bytes = f.read() - bytesio_images.append(BytesIO(image_bytes)) - return bytesio_images + return _make_test_images() class TestOpenAIImageEditGPTImage1(BaseLLMImageEditTest): @@ -129,7 +164,7 @@ class TestOpenAIImageEditGPTImage1(BaseLLMImageEditTest): """Return base call args for OpenAI image edit""" return { "model": "gpt-image-1", - "image": TEST_IMAGES, + "image": _make_test_images(), } @@ -143,7 +178,7 @@ class TestAzureAIFlux2ImageEdit(BaseLLMImageEditTest): """Return base call args for Azure AI FLUX 2 image edit""" return { "model": "azure_ai/flux.2-pro", - "image": SINGLE_TEST_IMAGE, + "image": _make_single_test_image(), "api_base": os.getenv("AZURE_AI_API_BASE"), "api_key": os.getenv("AZURE_AI_API_KEY"), "api_version": "preview", @@ -171,7 +206,7 @@ async def test_openai_image_edit_litellm_router(): result = await router.aimage_edit( prompt=prompt, model="gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) print("result from image edit", result) @@ -275,7 +310,7 @@ async def test_azure_image_edit_litellm_sdk(): api_base=test_api_base, api_key=test_api_key, api_version=test_api_version, - image=TEST_IMAGES, + image=_make_test_images(), ) # Verify the request was made correctly @@ -386,7 +421,7 @@ async def test_openai_image_edit_cost_tracking(): result = await aimage_edit( prompt=prompt, model="openai/gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) # Verify the request was made correctly @@ -477,7 +512,7 @@ async def test_azure_image_edit_cost_tracking(): prompt=prompt, model="azure/CUSTOM_AZURE_DEPLOYMENT_NAME", base_model="azure/gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) # Verify the request was made correctly @@ -525,7 +560,6 @@ async def test_recraft_image_edit_api(): import requests litellm._turn_on_debug() - global TEST_IMAGES try: prompt = """ Create a studio ghibli style image that combines all the reference images. Make sure the person looks like a CTO. @@ -533,7 +567,7 @@ async def test_recraft_image_edit_api(): result = await aimage_edit( prompt=prompt, model="recraft/recraftv3", - image=TEST_IMAGES, + image=_make_test_images(), ) print("result from image edit", result) @@ -631,13 +665,13 @@ async def test_multiple_vs_single_image_edit(sync_mode): single_result = image_edit( prompt=prompt, model="gpt-image-1", - image=SINGLE_TEST_IMAGE, + image=_make_single_test_image(), ) else: single_result = await aimage_edit( prompt=prompt, model="gpt-image-1", - image=SINGLE_TEST_IMAGE, + image=_make_single_test_image(), ) print("Single image result:", single_result) @@ -648,13 +682,13 @@ async def test_multiple_vs_single_image_edit(sync_mode): multiple_result = image_edit( prompt=prompt, model="gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) else: multiple_result = await aimage_edit( prompt=prompt, model="gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) print("Multiple images result:", multiple_result) @@ -685,7 +719,7 @@ async def test_multiple_image_edit_with_different_formats(): # Test with mixed BytesIO and file objects mixed_images = [ - SINGLE_TEST_IMAGE, # File object + _make_single_test_image(), # File object get_test_images_as_bytesio()[1], # BytesIO object ] @@ -749,14 +783,14 @@ async def test_image_edit_array_handling(): result1 = await aimage_edit( prompt=prompt, model="gpt-image-1", - image=SINGLE_TEST_IMAGE, + image=_make_single_test_image(), ) # Test 2: Multiple images (already a list) result2 = await aimage_edit( prompt=prompt, model="gpt-image-1", - image=TEST_IMAGES, + image=_make_test_images(), ) # Both valid calls should succeed