mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-13 23:11:40 +00:00
image control added
This commit is contained in:
parent
41cdf5fb4e
commit
6ef0eed7c7
2 changed files with 32 additions and 0 deletions
|
|
@ -50,6 +50,16 @@ class ImageURLContent(BaseModel):
|
|||
class ImageContent(BaseModel):
|
||||
type_: Literal["image_url"] = Field(default="image_url", alias="type")
|
||||
image_url: ImageURLContent
|
||||
cache_control: CacheControl | None = None
|
||||
|
||||
@model_serializer(mode="wrap")
|
||||
def _serialize(
|
||||
self, handler: SerializerFunctionWrapHandler, info: SerializationInfo
|
||||
) -> dict: # mutable-ok: pydantic serializer contract requires bare dict return
|
||||
result = handler(self)
|
||||
if result.get("cache_control") is None:
|
||||
result.pop("cache_control", None)
|
||||
return result
|
||||
|
||||
|
||||
class FunctionObj(BaseModel):
|
||||
|
|
|
|||
|
|
@ -919,3 +919,25 @@ class TestCacheControl:
|
|||
content = self._template(body)[0]["content"]
|
||||
assert "cache_control" not in content[0]
|
||||
assert content[1].get("cache_control") == {"type": "ephemeral"}
|
||||
|
||||
def test_message_level_cache_control_folded_onto_trailing_image_block(self):
|
||||
"""A top-level cache_control with a trailing image part lands on that image.
|
||||
|
||||
The Anthropic spec allows cache_control on any content block including
|
||||
image_url. Previously ImageContent had no cache_control field, so the
|
||||
marker was silently dropped by Pydantic validation.
|
||||
"""
|
||||
messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "text": "Describe this image."},
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc="}},
|
||||
],
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
]
|
||||
body = self._transform("anthropic--claude-3-5-sonnet", messages)
|
||||
content = self._template(body)[0]["content"]
|
||||
assert "cache_control" not in content[0]
|
||||
assert content[1].get("cache_control") == {"type": "ephemeral"}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue