Skip to content

AudioBase64Content

Base classes

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

⋔ Inheritance diagram

graph TD
  94376906390960["content.AudioBase64Content"]
  94376906940912["content.AudioContent"]
  94376906438544["content.BaseContent"]
  94376871297088["schema.Schema"]
  94376872608160["main.BaseModel"]
  140612526616800["builtins.object"]
  94376906940912 --> 94376906390960
  94376906438544 --> 94376906940912
  94376871297088 --> 94376906438544
  94376872608160 --> 94376871297088
  140612526616800 --> 94376872608160

🛈 DocStrings

Bases: AudioContent

Audio from base64 data.

Source code in src/llmling_agent/models/content.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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_openai_format(self) -> dict[str, Any]:
        """Convert to OpenAI API format for audio models."""
        data_url = f"data:audio/{self.format or 'mp3'};base64,{self.data}"
        content = {"url": data_url, "format": self.format or "auto"}
        return {"type": "audio", "audio": content}

    @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: StrPath) -> Self:
        """Create from file path with auto format detection."""
        import mimetypes

        from upath import UPath

        path_obj = 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)

data instance-attribute

data: str

Audio data in base64 format.

format class-attribute instance-attribute

format: str | None = None

Audio format.

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
269
270
271
272
@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: StrPath) -> Self

Create from file path with auto format detection.

Source code in src/llmling_agent/models/content.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@classmethod
def from_path(cls, path: StrPath) -> Self:
    """Create from file path with auto format detection."""
    import mimetypes

    from upath import UPath

    path_obj = 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)

to_openai_format

to_openai_format() -> dict[str, Any]

Convert to OpenAI API format for audio models.

Source code in src/llmling_agent/models/content.py
263
264
265
266
267
def to_openai_format(self) -> dict[str, Any]:
    """Convert to OpenAI API format for audio models."""
    data_url = f"data:audio/{self.format or 'mp3'};base64,{self.data}"
    content = {"url": data_url, "format": self.format or "auto"}
    return {"type": "audio", "audio": content}

Show source on GitHub