update image content item base64

This commit is contained in:
Xi Yan 2025-01-30 13:54:19 -08:00
parent 0d96070af9
commit 393d94429c
4 changed files with 39 additions and 19 deletions

View file

@ -2439,7 +2439,8 @@
"type": { "type": {
"type": "string", "type": "string",
"const": "image", "const": "image",
"default": "image" "default": "image",
"description": "Discriminator type of the content item. Always \"image\""
}, },
"image": { "image": {
"type": "object", "type": "object",
@ -2448,18 +2449,19 @@
"$ref": "#/components/schemas/URL" "$ref": "#/components/schemas/URL"
}, },
"data": { "data": {
"type": "string", "type": "string"
"contentEncoding": "base64"
} }
}, },
"additionalProperties": false "additionalProperties": false,
"description": "Image as a base64 encoded string or an URL"
} }
}, },
"additionalProperties": false, "additionalProperties": false,
"required": [ "required": [
"type", "type",
"image" "image"
] ],
"title": "A image content item"
}, },
"InterleavedContent": { "InterleavedContent": {
"oneOf": [ "oneOf": [
@ -2647,17 +2649,20 @@
"type": { "type": {
"type": "string", "type": "string",
"const": "text", "const": "text",
"default": "text" "default": "text",
"description": "Discriminator type of the content item. Always \"text\""
}, },
"text": { "text": {
"type": "string" "type": "string",
"description": "Text content"
} }
}, },
"additionalProperties": false, "additionalProperties": false,
"required": [ "required": [
"type", "type",
"text" "text"
] ],
"title": "A text content item"
}, },
"ToolCall": { "ToolCall": {
"type": "object", "type": "object",

View file

@ -1466,6 +1466,8 @@ components:
type: string type: string
const: image const: image
default: image default: image
description: >-
Discriminator type of the content item. Always "image"
image: image:
type: object type: object
properties: properties:
@ -1473,12 +1475,14 @@ components:
$ref: '#/components/schemas/URL' $ref: '#/components/schemas/URL'
data: data:
type: string type: string
contentEncoding: base64
additionalProperties: false additionalProperties: false
description: >-
Image as a base64 encoded string or an URL
additionalProperties: false additionalProperties: false
required: required:
- type - type
- image - image
title: A image content item
InterleavedContent: InterleavedContent:
oneOf: oneOf:
- type: string - type: string
@ -1598,12 +1602,16 @@ components:
type: string type: string
const: text const: text
default: text default: text
description: >-
Discriminator type of the content item. Always "text"
text: text:
type: string type: string
description: Text content
additionalProperties: false additionalProperties: false
required: required:
- type - type
- text - text
title: A text content item
ToolCall: ToolCall:
type: object type: object
properties: properties:

View file

@ -4,14 +4,13 @@
# This source code is licensed under the terms described in the LICENSE file in # This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree. # the root directory of this source tree.
import base64
from enum import Enum from enum import Enum
from typing import Annotated, List, Literal, Optional, Union from typing import Annotated, List, Literal, Optional, Union
from llama_models.llama3.api.datatypes import ToolCall from llama_models.llama3.api.datatypes import ToolCall
from llama_models.schema_utils import json_schema_type, register_schema from llama_models.schema_utils import json_schema_type, register_schema
from pydantic import BaseModel, Field, field_serializer, model_validator from pydantic import BaseModel, Field, model_validator
@json_schema_type @json_schema_type
@ -21,7 +20,8 @@ class URL(BaseModel):
class _URLOrData(BaseModel): class _URLOrData(BaseModel):
url: Optional[URL] = None url: Optional[URL] = None
data: Optional[bytes] = None # data is a base64 encoded string
data: Optional[str] = None
@model_validator(mode="before") @model_validator(mode="before")
@classmethod @classmethod
@ -30,21 +30,27 @@ class _URLOrData(BaseModel):
return values return values
return {"url": values} return {"url": values}
@field_serializer("data")
def serialize_data(self, data: Optional[bytes], _info):
if data is None:
return None
return base64.b64encode(data).decode("utf-8")
@json_schema_type @json_schema_type
class ImageContentItem(BaseModel): class ImageContentItem(BaseModel):
"""A image content item
:param type: Discriminator type of the content item. Always "image"
:param image: Image as a base64 encoded string or an URL
"""
type: Literal["image"] = "image" type: Literal["image"] = "image"
image: _URLOrData image: _URLOrData
@json_schema_type @json_schema_type
class TextContentItem(BaseModel): class TextContentItem(BaseModel):
"""A text content item
:param type: Discriminator type of the content item. Always "text"
:param text: Text content
"""
type: Literal["text"] = "text" type: Literal["text"] = "text"
text: str text: str

View file

@ -135,7 +135,8 @@ async def interleaved_content_convert_to_raw(
else: else:
raise ValueError("Unsupported URL type") raise ValueError("Unsupported URL type")
elif image.data: elif image.data:
data = image.data # data is a base64 encoded string, decode it to bytes for RawMediaItem
data = base64.b64decode(image.data)
else: else:
raise ValueError("No data or URL provided") raise ValueError("No data or URL provided")