sdk-python
sdk-python copied to clipboard
feat(types): add native audio content block support
Description
Add native audio content block support to the SDK's type system, following the established pattern for video, image, and document content.
Changes
-
Types (
src/strands/types/media.py):- Add
AudioFormatliteral type (mp3,wav,flac,ogg,webm) - Add
AudioSourceTypedDict withbytesattribute - Add
AudioContentTypedDict withformatandsourceattributes
- Add
-
ContentBlock (
src/strands/types/content.py):- Add
audio: AudioContentfield to theContentBlockTypedDict
- Add
-
Bedrock Provider (
src/strands/models/bedrock.py):- Add audio handling in
_format_request_message_content()following the video pattern
- Add audio handling in
-
LlamaCpp Provider (
src/strands/models/llamacpp.py):- Update to use native
AudioContenttypes instead ofcast(Dict[str, Any], content)
- Update to use native
-
Tests (
tests/strands/models/test_bedrock.py):- Add
test_format_request_filters_audio_content_blockstest
- Add
Benefits
- Type Safety: No more
cast(Dict[str, Any], content)workarounds - Consistency: Audio follows the same pattern as video, image, and document
- Model Support: Enables type-safe audio handling for Bedrock (Nova Sonic), LlamaCpp (Qwen2.5-Omni), and future providers
Usage Example
from strands.types.content import ContentBlock
from strands.types.media import AudioContent
audio_block: ContentBlock = {
"audio": {
"format": "mp3",
"source": {"bytes": audio_bytes}
}
}
response = agent([audio_block, {"text": "What do you hear in this audio?"}])
Related Issues
Closes #866
Documentation PR
No documentation changes required - this adds types that follow existing patterns.
Type of Change
New feature
Testing
- [x] I ran
hatch run prepare(formatter + linter + mypy + tests) - [x] Unit test passes:
test_format_request_filters_audio_content_blocks - [x] All existing tests continue to pass
Checklist
- [x] I have read the CONTRIBUTING document
- [x] I have added any necessary tests that prove my fix is effective or my feature works
- [x] I have updated the documentation accordingly
- [x] I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
- [x] My changes generate no new warnings
- [x] Any dependent changes have been merged and published