Created
November 30, 2025 16:30
-
-
Save dsfaccini/464b649677a1261690e95fe5a265f90c to your computer and use it in GitHub Desktop.
This script is used to reproduce the bug that existed before where `pydantic_ai._mcp.map_from_pai_messages()` was calling `base64.b64decode(chunk.data)` on `chunk.data: bytes`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """Quick script to verify base64 encoding/decoding for MCP content using actual classes.""" | |
| from pydantic_ai._mcp import map_from_pai_messages | |
| from pydantic_ai.messages import BinaryContent, ModelRequest, UserPromptPart | |
| # Test image content | |
| raw_image_bytes = b'some image data here' | |
| image_message = ModelRequest( | |
| parts=[ | |
| UserPromptPart( | |
| content=[BinaryContent(data=raw_image_bytes, media_type='image/png')] | |
| ) | |
| ] | |
| ) | |
| print('=== Testing map_from_pai_messages with image content ===') | |
| print(f'Input BinaryContent.data: {raw_image_bytes!r}') | |
| try: | |
| _, sampling_msgs = map_from_pai_messages([image_message]) | |
| print(f'Success! Content: {sampling_msgs[0].content}') | |
| except Exception as e: | |
| print(f'ERROR: {type(e).__name__}: {e}') | |
| print() | |
| # Test audio content | |
| raw_audio_bytes = b'some audio data here' | |
| audio_message = ModelRequest( | |
| parts=[ | |
| UserPromptPart( | |
| content=[BinaryContent(data=raw_audio_bytes, media_type='audio/wav')] | |
| ) | |
| ] | |
| ) | |
| print('=== Testing map_from_pai_messages with audio content ===') | |
| print(f'Input BinaryContent.data: {raw_audio_bytes!r}') | |
| try: | |
| _, sampling_msgs = map_from_pai_messages([audio_message]) | |
| print(f'Success! Content: {sampling_msgs[0].content}') | |
| except Exception as e: | |
| print(f'ERROR: {type(e).__name__}: {e}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment