image control added

This commit is contained in:
Yamac Ay 2026-09-07 10:28:48 +02:00
parent 41cdf5fb4e
commit 6ef0eed7c7
No known key found for this signature in database
GPG key ID: D113B438819CA628
2 changed files with 32 additions and 0 deletions

View file

@ -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):

View file

@ -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"}