classAudioBase64Content(AudioContent):"""Audio from base64 data."""type:Literal["audio_base64"]=Field("audio_base64",init=False)"""Base64-encoded audio."""data:str"""Audio data in base64 format."""format:str|None=None# mp3, wav, etc"""Audio format."""defto_pydantic_ai(self)->BinaryContent:binary_data=base64.b64decode(self.data)returnBinaryContent(data=binary_data,media_type=f"audio/{self.formator'mp3'}")@classmethoddeffrom_bytes(cls,data:bytes,audio_format:str="mp3")->Self:"""Create from raw bytes."""returncls(data=base64.b64encode(data).decode(),format=audio_format)@classmethoddeffrom_path(cls,path:JoinablePathLike)->Self:"""Create from file path with auto format detection."""importmimetypesfromupathtoolsimportto_upathpath_obj=to_upath(path)mime_type,_=mimetypes.guess_type(str(path_obj))fmt=(mime_type.removeprefix("audio/")ifmime_typeandmime_type.startswith("audio/")else"mp3")returncls(data=base64.b64encode(path_obj.read_bytes()).decode(),format=fmt)@propertydefmime_type(self)->str:"""Return the MIME type of the audio."""returnf"audio/{self.formator'mp3'}"
Source code in src/llmling_agent/models/content.py
260261262263
@classmethoddeffrom_bytes(cls,data:bytes,audio_format:str="mp3")->Self:"""Create from raw bytes."""returncls(data=base64.b64encode(data).decode(),format=audio_format)
Source code in src/llmling_agent/models/content.py
265266267268269270271272273274275276277278279280
@classmethoddeffrom_path(cls,path:JoinablePathLike)->Self:"""Create from file path with auto format detection."""importmimetypesfromupathtoolsimportto_upathpath_obj=to_upath(path)mime_type,_=mimetypes.guess_type(str(path_obj))fmt=(mime_type.removeprefix("audio/")ifmime_typeandmime_type.startswith("audio/")else"mp3")returncls(data=base64.b64encode(path_obj.read_bytes()).decode(),format=fmt)
Source code in src/llmling_agent/models/content.py
221222223224225226227228
classAudioContent(BaseContent):"""Base for audio content."""format:str|None=None# mp3, wav, etc"""Audio format."""description:str|None=None"""Human-readable description of the content."""
Source code in src/llmling_agent/models/content.py
231232233234235236237238239240241
classAudioURLContent(AudioContent):"""Audio from URL."""type:Literal["audio_url"]=Field("audio_url",init=False)"""URL-based audio."""url:str"""URL to the audio."""defto_pydantic_ai(self)->AudioUrl:returnAudioUrl(url=self.url)
Source code in src/llmling_agent/models/content.py
3031323334353637383940414243
classBaseContent(Schema):"""Base class for special content types (non-text)."""type:str=Field(init=False)"""Discriminator field for content types."""description:str|None=None"""Human-readable description of the content."""model_config=ConfigDict(frozen=True)defto_pydantic_ai(self)->UserContent:msg="Subclasses must implement this method"raiseNotImplementedError(msg)
classBaseImageContent(BaseContent):"""Base for image content."""detail:DetailLevel|None=None"""Detail level for image processing by vision models. - high: Maximum resolution (up to 2048x2048) - low: Lower resolution (512x512) - auto: Let model decide based on content """@classmethodasyncdeffrom_path(cls,path:JoinablePathLike,*,detail:DetailLevel|None=None,description:str|None=None,)->ImageURLContent|ImageBase64Content:"""Create image content from any path. Automatically chooses between URL and base64 based on path type. Downloads and converts remote content if needed. Args: path: Local path or URL to image detail: Optional detail level for processing description: Optional description of the image """fromupathtoolsimportto_upathpath_obj=to_upath(path)# For http(s) URLs, pass through as URL contentifpath_obj.protocolin{"http","https"}:returnImageURLContent(url=str(path_obj),detail=detail,description=description)# For all other paths, read and convert to base64data=awaitread_path(path_obj,mode="rb")content=base64.b64encode(data).decode()returnImageBase64Content(data=content,detail=detail,description=description)
Detail level for image processing by vision models.
- high: Maximum resolution (up to 2048x2048)
- low: Lower resolution (512x512)
- auto: Let model decide based on content
@classmethodasyncdeffrom_path(cls,path:JoinablePathLike,*,detail:DetailLevel|None=None,description:str|None=None,)->ImageURLContent|ImageBase64Content:"""Create image content from any path. Automatically chooses between URL and base64 based on path type. Downloads and converts remote content if needed. Args: path: Local path or URL to image detail: Optional detail level for processing description: Optional description of the image """fromupathtoolsimportto_upathpath_obj=to_upath(path)# For http(s) URLs, pass through as URL contentifpath_obj.protocolin{"http","https"}:returnImageURLContent(url=str(path_obj),detail=detail,description=description)# For all other paths, read and convert to base64data=awaitread_path(path_obj,mode="rb")content=base64.b64encode(data).decode()returnImageBase64Content(data=content,detail=detail,description=description)
classBasePDFContent(BaseContent):"""Base for PDF document content."""detail:DetailLevel|None=None"""Detail level for document processing by models."""@classmethodasyncdeffrom_path(cls,path:JoinablePathLike,*,detail:DetailLevel|None=None,description:str|None=None,)->PDFURLContent|PDFBase64Content:"""Create PDF content from any path. Args: path: Local path or URL to PDF detail: Optional detail level for processing description: Optional description of the document """fromupathtoolsimportto_upathpath_obj=to_upath(path)# For http(s) URLs, pass through as URL contentifpath_obj.protocolin{"http","https"}:returnPDFURLContent(url=str(path_obj),detail=detail,description=description)# For all other paths, read and convert to base64data=awaitread_path(path_obj,mode="rb")content=base64.b64encode(data).decode()returnPDFBase64Content(data=content,detail=detail,description=description)
@classmethodasyncdeffrom_path(cls,path:JoinablePathLike,*,detail:DetailLevel|None=None,description:str|None=None,)->PDFURLContent|PDFBase64Content:"""Create PDF content from any path. Args: path: Local path or URL to PDF detail: Optional detail level for processing description: Optional description of the document """fromupathtoolsimportto_upathpath_obj=to_upath(path)# For http(s) URLs, pass through as URL contentifpath_obj.protocolin{"http","https"}:returnPDFURLContent(url=str(path_obj),detail=detail,description=description)# For all other paths, read and convert to base64data=awaitread_path(path_obj,mode="rb")content=base64.b64encode(data).decode()returnPDFBase64Content(data=content,detail=detail,description=description)
classImageBase64Content(BaseImageContent):"""Image from base64 data."""type:Literal["image_base64"]=Field("image_base64",init=False)"""Base64-encoded image."""data:str"""Base64-encoded image data."""mime_type:str="image/jpeg""""MIME type of the image."""defto_pydantic_ai(self)->BinaryImage:binary_data=base64.b64decode(self.data)returnBinaryImage(data=binary_data,media_type=self.mime_typeor"image/jpeg")@classmethoddeffrom_bytes(cls,data:bytes,*,detail:DetailLevel|None=None,description:str|None=None,)->ImageBase64Content:"""Create image content from raw bytes. Args: data: Raw image bytes detail: Optional detail level for processing description: Optional description of the image """content=base64.b64encode(data).decode()returncls(data=content,detail=detail,description=description)@classmethoddeffrom_pil_image(cls,image:PIL.Image.Image)->ImageBase64Content:"""Create content from PIL Image."""withio.BytesIO()asbuffer:image.save(buffer,format="PNG")returncls(data=base64.b64encode(buffer.getvalue()).decode())
@classmethoddeffrom_bytes(cls,data:bytes,*,detail:DetailLevel|None=None,description:str|None=None,)->ImageBase64Content:"""Create image content from raw bytes. Args: data: Raw image bytes detail: Optional detail level for processing description: Optional description of the image """content=base64.b64encode(data).decode()returncls(data=content,detail=detail,description=description)
Source code in src/llmling_agent/models/content.py
137138139140141142
@classmethoddeffrom_pil_image(cls,image:PIL.Image.Image)->ImageBase64Content:"""Create content from PIL Image."""withio.BytesIO()asbuffer:image.save(buffer,format="PNG")returncls(data=base64.b64encode(buffer.getvalue()).decode())
Source code in src/llmling_agent/models/content.py
90 91 92 93 94 95 96 97 98 99100
classImageURLContent(BaseImageContent):"""Image from URL."""type:Literal["image_url"]=Field("image_url",init=False)"""URL-based image."""url:str"""URL to the image."""defto_pydantic_ai(self)->ImageUrl:returnImageUrl(url=self.url)
classPDFBase64Content(BasePDFContent):"""PDF from base64 data."""type:Literal["pdf_base64"]=Field("pdf_base64",init=False)"""Base64-data based PDF."""data:str"""Base64-encoded PDF data."""@classmethoddeffrom_bytes(cls,data:bytes,*,detail:DetailLevel|None=None,description:str|None=None,)->Self:"""Create PDF content from raw bytes."""content=base64.b64encode(data).decode()returncls(data=content,detail=detail,description=description)defto_pydantic_ai(self)->BinaryContent:binary_data=base64.b64decode(self.data)returnBinaryContent(binary_data,media_type="application/pdf")
Source code in src/llmling_agent/models/content.py
204205206207208209210211212213214
@classmethoddeffrom_bytes(cls,data:bytes,*,detail:DetailLevel|None=None,description:str|None=None,)->Self:"""Create PDF content from raw bytes."""content=base64.b64encode(data).decode()returncls(data=content,detail=detail,description=description)
Source code in src/llmling_agent/models/content.py
182183184185186187188189190191192
classPDFURLContent(BasePDFContent):"""PDF from URL."""type:Literal["pdf_url"]=Field("pdf_url",init=False)"""URL-based PDF."""url:str"""URL to the PDF document."""defto_pydantic_ai(self)->DocumentUrl:returnDocumentUrl(url=self.url)
Source code in src/llmling_agent/models/content.py
288289290291292293294295
classVideoContent(BaseContent):"""Base for video content."""format:str|None=None"""Video format."""description:str|None=None"""Human-readable description of the content."""
Source code in src/llmling_agent/models/content.py
298299300301302303304305
classVideoURLContent(VideoContent):"""Video from URL."""type:Literal["video_url"]=Field("video_url",init=False)"""URL-based video."""url:str"""URL to the video."""