dspy.Audio¶
dspy.Audio(*args, **data)
¶
Bases: Type
An audio input type for DSPy.
Construct from in-memory values only: a data URI string, raw bytes (with
audio_format=), a numpy-like array, a {"data", "audio_format"} dict, or another
Audio. Raw base64 is passed as Audio(data=..., audio_format=...). Construction and
adapter parsing never access the filesystem or network; use :meth:from_path to read a local
file, :meth:from_url to download a remote resource, or :meth:from_array for array data.
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
Methods:¶
adapt_to_native_lm_feature(signature: type[Signature], field_name: str, lm: BaseLM, lm_kwargs: dict[str, Any]) -> type[Signature]
classmethod
¶
Adapt the custom type to the native LM feature if possible.
When the LM and configuration supports the related native LM feature, e.g., native tool calling, native
reasoning, etc., we adapt the signature and lm_kwargs to enable the native LM feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for the LM call. |
required |
field_name
|
str
|
The name of the field in the signature to adapt to the native LM feature. |
required |
lm
|
BaseLM
|
The LM instance. |
required |
lm_kwargs
|
dict[str, Any]
|
The keyword arguments for the LM call, subject to in-place updates if adaptation if required. |
required |
Returns:
| Type | Description |
|---|---|
type[Signature]
|
The adapted signature. If the custom type is not natively supported by the LM, return the original |
type[Signature]
|
signature. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/base_type.py
description() -> str
classmethod
¶
extract_custom_type_from_annotation(annotation)
classmethod
¶
Extract all custom types from the annotation.
This is used to extract all custom types from the annotation of a field, while the annotation can
have arbitrary level of nesting. For example, we detect Tool is in list[dict[str, Tool]].
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/base_type.py
format() -> list[dict[str, Any]]
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
from_array(array: Any, sampling_rate: int, format: str = 'wav') -> Audio
classmethod
¶
Process numpy-like array and encode it as base64. Uses sampling rate and audio format for encoding.
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
from_file(file_path: str) -> Audio
classmethod
¶
Deprecated alias for :meth:from_path.
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
from_path(file_path: str) -> Audio
classmethod
¶
Read local audio file and encode it as base64.
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
from_url(url: str, verify: bool = True) -> Audio
classmethod
¶
Download an audio file from URL and encode it as base64.
Security: this performs an explicit, caller-initiated fetch and applies no
SSRF protection beyond requiring an HTTP(S) scheme. Like requests.get, it
will reach private, loopback, or cloud-metadata hosts and follow redirects to
them. When url is derived from untrusted input, the caller is responsible
for validating the host against an allowlist before calling this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the audio to download. |
required |
verify
|
bool
|
Whether to verify SSL certificates. Set to False for self-signed certs. |
True
|
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/audio.py
is_streamable() -> bool
classmethod
¶
parse_lm_response(response: str | dict[str, Any]) -> Optional[Type]
classmethod
¶
Parse a LM response into the custom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str | dict[str, Any]
|
A LM response. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Type]
|
A custom type object. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/base_type.py
parse_stream_chunk(chunk: ModelResponseStream) -> Optional[Type]
classmethod
¶
Parse a stream chunk into the custom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
ModelResponseStream
|
A stream chunk. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Type]
|
A custom type object or None if the chunk is not for this custom type. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/base_type.py
serialize_model()
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/types/base_type.py
validate_input(values: Any) -> Any
classmethod
¶
Validate input for Audio, expecting ‘data’ and ‘audio_format’ keys in dictionary.