The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
test(image_edits): regenerate fixtures per call instead of holding open module-level file handles
Cursor Agent656377bb3df4
diff --git a/tests/image_gen_tests/test_image_edits.py b/tests/image_gen_tests/test_image_edits.pyindex 6900bacdb90..441511b0141 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 runpwd = 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 requestslitellm._turn_on_debug()- global TEST_IMAGEStry: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 objectsmixed_images = [- SINGLE_TEST_IMAGE, # File object+ _make_single_test_image(), # File objectget_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