Skip to content

AudioBase64Content

Base classes

Name Children Inherits
AudioContent
llmling_agent.models.content
Base for audio content.

⋔ Inheritance diagram

graph TD
  94123202044224["content.AudioBase64Content"]
  94123202225472["content.AudioContent"]
  94123202050832["content.BaseContent"]
  94123169468080["schema.Schema"]
  94123157077184["main.BaseModel"]
  139872072243680["builtins.object"]
  94123202225472 --> 94123202044224
  94123202050832 --> 94123202225472
  94123169468080 --> 94123202050832
  94123157077184 --> 94123169468080
  139872072243680 --> 94123157077184

🛈 DocStrings

Bases: AudioContent

Audio from base64 data.

Source code in src/llmling_agent/models/content.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
class AudioBase64Content(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."""

    def to_pydantic_ai(self) -> BinaryContent:
        binary_data = base64.b64decode(self.data)
        return BinaryContent(data=binary_data, media_type=f"audio/{self.format or 'mp3'}")

    @classmethod
    def from_bytes(cls, data: bytes, audio_format: str = "mp3") -> Self:
        """Create from raw bytes."""
        return cls(data=base64.b64encode(data).decode(), format=audio_format)

    @classmethod
    def from_path(cls, path: JoinablePathLike) -> Self:
        """Create from file path with auto format detection."""
        import mimetypes

        from upathtools import to_upath

        path_obj = to_upath(path)
        mime_type, _ = mimetypes.guess_type(str(path_obj))
        fmt = (
            mime_type.removeprefix("audio/")
            if mime_type and mime_type.startswith("audio/")
            else "mp3"
        )

        return cls(data=base64.b64encode(path_obj.read_bytes()).decode(), format=fmt)

    @property
    def mime_type(self) -> str:
        """Return the MIME type of the audio."""
        return f"audio/{self.format or 'mp3'}"

data instance-attribute

data: str

Audio data in base64 format.

format class-attribute instance-attribute

format: str | None = None

Audio format.

mime_type property

mime_type: str

Return the MIME type of the audio.

type class-attribute instance-attribute

type: Literal['audio_base64'] = Field('audio_base64', init=False)

Base64-encoded audio.

from_bytes classmethod

from_bytes(data: bytes, audio_format: str = 'mp3') -> Self

Create from raw bytes.

Source code in src/llmling_agent/models/content.py
241
242
243
244
@classmethod
def from_bytes(cls, data: bytes, audio_format: str = "mp3") -> Self:
    """Create from raw bytes."""
    return cls(data=base64.b64encode(data).decode(), format=audio_format)

from_path classmethod

from_path(path: JoinablePathLike) -> Self

Create from file path with auto format detection.

Source code in src/llmling_agent/models/content.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
@classmethod
def from_path(cls, path: JoinablePathLike) -> Self:
    """Create from file path with auto format detection."""
    import mimetypes

    from upathtools import to_upath

    path_obj = to_upath(path)
    mime_type, _ = mimetypes.guess_type(str(path_obj))
    fmt = (
        mime_type.removeprefix("audio/")
        if mime_type and mime_type.startswith("audio/")
        else "mp3"
    )

    return cls(data=base64.b64encode(path_obj.read_bytes()).decode(), format=fmt)

Show source on GitHub