66from pydantic import BaseModel , ConfigDict , Field , model_validator
77
88from .tool_call import ToolCall
9- from ..enumeration import Role
9+ from ..enumeration import Role , ContentBlockType
1010
1111
1212class ContentBlock (BaseModel ):
@@ -35,7 +35,7 @@ class ContentBlock(BaseModel):
3535
3636 model_config = ConfigDict (extra = "allow" )
3737
38- type : str = Field (default = "" )
38+ type : ContentBlockType = Field (default = "" )
3939 content : str | dict | list = Field (default = "" )
4040
4141 @model_validator (mode = "before" )
@@ -44,14 +44,15 @@ def init_block(cls, data: dict):
4444 """Initialize content block by extracting content based on type field."""
4545 result = data .copy ()
4646 content_type = data .get ("type" , "" )
47- result ["content" ] = data [content_type ]
47+ if content_type and content_type in data :
48+ result ["content" ] = data [content_type ]
4849 return result
4950
5051 def simple_dump (self ) -> dict :
5152 """Convert ContentBlock to a simple dictionary format."""
5253 result = {
53- "type" : self .type ,
54- self .type : self .content ,
54+ "type" : self .type . value ,
55+ self .type . value : self .content ,
5556 ** self .model_extra ,
5657 }
5758
0 commit comments