fix(memory): guard against malformed image_url in parse_vision_messages (#5631)

This commit is contained in:
Yash Singh
2026-06-24 11:10:36 +05:30
committed by GitHub
parent fee344db85
commit 25bc1b7426
3 changed files with 27 additions and 4 deletions
+18
View File
@@ -96,6 +96,24 @@ class TestParseVisionMessages:
result = parse_vision_messages(messages, llm=None)
assert result == messages
def test_malformed_image_dict_raises_value_error(self):
# A malformed image part (missing the nested url) used to raise an
# uncaught KeyError that aborted add(); it should raise a clear ValueError.
mock_llm = Mock()
messages = [{"role": "user", "content": {"type": "image_url", "image_url": {}}}]
with pytest.raises(ValueError, match=r"missing image_url\.url"):
parse_vision_messages(messages, llm=mock_llm)
mock_llm.generate_response.assert_not_called()
def test_none_image_url_raises_value_error(self):
# image_url present but None (or any non-dict) must also raise the clear
# ValueError, not an AttributeError from calling .get() on None.
mock_llm = Mock()
messages = [{"role": "user", "content": {"type": "image_url", "image_url": None}}]
with pytest.raises(ValueError, match=r"missing image_url\.url"):
parse_vision_messages(messages, llm=mock_llm)
mock_llm.generate_response.assert_not_called()
class TestRemoveSpacesFromEntities:
"""