mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(interactions): freeze new image parts to satisfy type-discipline gate, close coverage gaps
Wrap the two new input_image dict literals in MappingProxyType so they don't count as new LIT002 mutable-collection-construction violations against the repo's type-discipline budget, which is already at its ceiling. Also add tests for the webp signature, undecodable/too-short base64 data, and the no-data/no-uri passthrough, closing the remaining gaps in the new code.
This commit is contained in:
parent
e0795eac6d
commit
f77e5dca8a
2 changed files with 41 additions and 2 deletions
|
|
@ -201,7 +201,7 @@ class LiteLLMResponsesInteractionsConfig:
|
|||
def _transform_image_content_item(item: Mapping[str, object]) -> Mapping[str, object]:
|
||||
uri: Final = item.get("uri")
|
||||
if isinstance(uri, str) and uri:
|
||||
return {"type": "input_image", "image_url": uri}
|
||||
return MappingProxyType({"type": "input_image", "image_url": uri})
|
||||
|
||||
data: Final = item.get("data")
|
||||
if isinstance(data, str) and data:
|
||||
|
|
@ -210,7 +210,7 @@ class LiteLLMResponsesInteractionsConfig:
|
|||
or LiteLLMResponsesInteractionsConfig._sniff_image_mime_type(data)
|
||||
or "application/octet-stream"
|
||||
)
|
||||
return {"type": "input_image", "image_url": f"data:{mime_type};base64,{data}"}
|
||||
return MappingProxyType({"type": "input_image", "image_url": f"data:{mime_type};base64,{data}"})
|
||||
|
||||
return item
|
||||
|
||||
|
|
|
|||
|
|
@ -137,6 +137,45 @@ class TestBridgeInputTransformation:
|
|||
)
|
||||
assert transformed[0]["content"][0]["image_url"].startswith("data:application/octet-stream;base64,")
|
||||
|
||||
def test_image_content_webp_signature_is_sniffed_from_data(self):
|
||||
webp_signature_b64 = base64.b64encode(b"RIFF\x00\x00\x00\x00WEBPrest-of-file").decode()
|
||||
image_part = {"type": "image", "data": webp_signature_b64}
|
||||
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
|
||||
[{"type": "user_input", "content": [image_part]}]
|
||||
)
|
||||
assert transformed == [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [{"type": "input_image", "image_url": f"data:image/webp;base64,{webp_signature_b64}"}],
|
||||
}
|
||||
]
|
||||
|
||||
def test_image_content_with_undecodable_data_defaults_to_octet_stream(self):
|
||||
image_part = {"type": "image", "data": "a" + "!" * 23}
|
||||
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
|
||||
[{"type": "user_input", "content": [image_part]}]
|
||||
)
|
||||
assert transformed[0]["content"][0]["image_url"].startswith("data:application/octet-stream;base64,")
|
||||
|
||||
def test_image_content_with_too_short_data_defaults_to_octet_stream(self):
|
||||
image_part = {"type": "image", "data": "ab"}
|
||||
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
|
||||
[{"type": "user_input", "content": [image_part]}]
|
||||
)
|
||||
assert transformed == [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [{"type": "input_image", "image_url": "data:application/octet-stream;base64,ab"}],
|
||||
}
|
||||
]
|
||||
|
||||
def test_image_content_without_data_or_uri_passes_through_unchanged(self):
|
||||
image_part = {"type": "image", "mime_type": "image/png"}
|
||||
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
|
||||
[{"type": "user_input", "content": [image_part]}]
|
||||
)
|
||||
assert transformed == [{"role": "user", "content": [image_part]}]
|
||||
|
||||
def test_unrecognized_content_type_passes_through_unchanged(self):
|
||||
other_part = {"type": "document", "data": "base64data", "mime_type": "application/pdf"}
|
||||
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue