From 0ac435bd0195ec224c8399f0655ace3fd0d3c68d Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Thu, 24 Sep 2026 11:54:26 -0700 Subject: [PATCH] test(ollama): assert the images sent to Ollama instead of echoing them through response (#42905) test_ollama_image returned the request's images list as the mocked `response` field and read it back from message content. Since #42838 the completion transform validates `response` as a string, so the list is dropped and the test fails with "string index out of range". Ollama always sends a string there. The mock now returns a real string reply and the test asserts on the images the transform actually sent, which is what it was checking all along. --- tests/local_testing/test_completion.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/local_testing/test_completion.py b/tests/local_testing/test_completion.py index 25c6c50251d..c6dd78c73b4 100644 --- a/tests/local_testing/test_completion.py +++ b/tests/local_testing/test_completion.py @@ -1361,16 +1361,14 @@ def test_ollama_image(): from PIL import Image + sent_images = [] + def mock_post(url, **kwargs): + sent_images.append(json.loads(kwargs["data"])["images"]) mock_response = MagicMock() mock_response.status_code = 200 mock_response.headers = {"Content-Type": "application/json"} - data_json = json.loads(kwargs["data"]) - mock_response.json.return_value = { - # return the image in the response so that it can be tested - # against the original - "response": data_json["images"] - } + mock_response.json.return_value = {"response": "a black pixel"} return mock_response def make_b64image(format): @@ -1399,9 +1397,10 @@ def test_ollama_image(): client = HTTPHandler() for test in tests: + sent_images.clear() try: with patch.object(client, "post", side_effect=mock_post): - response = completion( + completion( model="ollama/llava", messages=[ { @@ -1417,14 +1416,14 @@ def test_ollama_image(): ], client=client, ) + (image_data,) = sent_images[0] if not test[1]: # the conversion process may not always generate the same image, # so just check for a JPEG image when a conversion was done. - image_data = response["choices"][0]["message"]["content"][0] image = Image.open(io.BytesIO(base64.b64decode(image_data))) assert image.format == "JPEG" else: - assert response["choices"][0]["message"]["content"][0] == test[1] + assert image_data == test[1] except Exception as e: pytest.fail(f"Error occurred: {e}")