Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/kosong/src/kosong/message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from abc import ABC
from typing import Any, ClassVar, Literal, cast, override

from pydantic import BaseModel, GetCoreSchemaHandler, field_serializer, field_validator
from pydantic import (
BaseModel,
GetCoreSchemaHandler,
SerializerFunctionWrapHandler,
field_serializer,
field_validator,
)
from pydantic_core import core_schema

from kosong.utils.typing import JsonType
Expand Down Expand Up @@ -262,11 +268,15 @@ class Message(BaseModel):

partial: bool | None = None

@field_serializer("content")
def _serialize_content(self, content: list[ContentPart]) -> str | list[dict[str, Any]] | None:
@field_serializer("content", mode="wrap")
def _serialize_content(
self,
content: list[ContentPart],
serializer: SerializerFunctionWrapHandler,
) -> str | list[dict[str, Any]] | None:
if len(content) == 1 and isinstance(content[0], TextPart):
return content[0].text
return [part.model_dump() for part in content]
return serializer(content)

@field_validator("content", mode="before")
@classmethod
Expand Down
2 changes: 0 additions & 2 deletions packages/kosong/tests/api_snapshot_tests/test_kimi.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ async def test_kimi_message_conversion():
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
"id": None,
},
},
],
Expand Down Expand Up @@ -204,7 +203,6 @@ async def test_kimi_message_conversion():
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
"id": None,
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ async def test_openai_legacy_message_conversion():
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
"id": None,
},
},
],
Expand Down Expand Up @@ -128,7 +127,6 @@ async def test_openai_legacy_message_conversion():
"type": "image_url",
"image_url": {
"url": "https://example.com/image.png",
"id": None,
},
},
],
Expand Down
54 changes: 45 additions & 9 deletions packages/kosong/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,54 @@ def test_message_with_single_part():
"content": [
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.png", "id": None},
"image_url": {"url": "https://example.com/image.png"},
}
],
}
)
assert Message.model_validate(dumped) == message


def test_exclude_none_applies_to_nested_media_content_parts():
message = Message(
role="user",
content=[
ImageURLPart(image_url=ImageURLPart.ImageURL(url="https://example.com/image.png")),
AudioURLPart(audio_url=AudioURLPart.AudioURL(url="https://example.com/audio.mp3")),
VideoURLPart(
video_url=VideoURLPart.VideoURL(
url="https://example.com/video.mp4",
id="video-1",
)
),
],
)

dumped_with_none = message.model_dump()
assert dumped_with_none["content"][0]["image_url"]["id"] is None

assert message.model_dump(exclude_none=True) == {
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.png"},
},
{
"type": "audio_url",
"audio_url": {"url": "https://example.com/audio.mp3"},
},
{
"type": "video_url",
"video_url": {
"url": "https://example.com/video.mp4",
"id": "video-1",
},
},
],
}


def test_message_with_tool_calls():
message = Message(
role="assistant",
Expand Down Expand Up @@ -107,22 +147,18 @@ def test_message_with_complex_content():
"role": "user",
"content": [
{"type": "text", "text": "Hello, world!"},
{
"type": "think",
"think": "I think I need to think about this.",
"encrypted": None,
},
{"type": "think", "think": "I think I need to think about this."},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.png", "id": None},
"image_url": {"url": "https://example.com/image.png"},
},
{
"type": "audio_url",
"audio_url": {"url": "https://example.com/audio.mp3", "id": None},
"audio_url": {"url": "https://example.com/audio.mp3"},
},
{
"type": "video_url",
"video_url": {"url": "https://example.com/video.mp4", "id": None},
"video_url": {"url": "https://example.com/video.mp4"},
},
],
"tool_calls": [
Expand Down
Loading