Repository navigation
Python: default DataUri to base64 for bytes so binary contents serialize - #14843
Open
Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
Open
Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
Conversation
A DataUri built from bytes or a numpy array without a data_format wrote the raw bytes as UTF-8 text, so BinaryContent, ImageContent and AudioContent created with data=<non-UTF-8 bytes> raised UnicodeDecodeError from data_uri, data_string, equality and ChatHistory.to_prompt(). Raw data now defaults to base64; strings without a format are still kept as text, and an explicit data_format is always respected. Fixes microsoft#14842 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Muhammad Abdullah Butt (1AbdullahButt)
requested a review
from a team
as a code owner
November 20, 2026 19:33
Muhammad Abdullah Butt (1AbdullahButt)
temporarily deployed
to
github-app-auth
November 20, 2026 19:33 — with
GitHub Actions
Inactive
Copilot started reviewing on behalf of
Muhammad Abdullah Butt (1AbdullahButt)
November 20, 2026 19:34
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the change that defaults binary DataUri content to base64.
No open issues 🎉
What changed in this PR
Addresses #14842 by making Python’s shared DataUri utility default raw bytes and arrays to base64, allowing binary, image, and audio content to serialize safely.
Changes:
- Applies base64 defaults during initialization and data updates, preserving explicit formats and text-string behavior.
- Adds regression tests for serialization, equality, data URI reconstruction, and image prompt round-trips.
- Updates the realtime audio test to expect a base64-encoded payload.
| File | Description |
|---|---|
python/tests/unit/contents/test_data_uri.py |
Tests byte and array defaults, unchanged string handling, and byte updates. |
python/tests/unit/contents/test_binary_content.py |
Tests non-UTF-8 content serialization and image prompt round-trips. |
python/tests/unit/connectors/ai/open_ai/services/test_openai_realtime.py |
Changes the expected audio payload to base64. |
python/semantic_kernel/contents/utils/data_uri.py |
Implements base64 defaults and documents the new behavior. |
Review effort: Balanced
This branch was previously deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #14842.
ImageContent(data=png_bytes, mime_type="image/png"), and the same call withAudioContentorBinaryContent, raisesUnicodeDecodeErrorfrom.data_uri,.data_string,==andChatHistory.to_prompt().DataUristores the bytes with nodata_format, so_data_str()decodes them as UTF-8. That only works when the bytes happen to be valid UTF-8, and in that casefrom_rendered_promptreads the content back as text.The connectors that read
data_string/data_uriall need base64 there: the realtimeinput_audio_buffer.append, the MCPdata/blobfields, the Responsesfile_dataURI (which already hard-codes;base64,), and the image URLs for Azure AI Inference, Google and the Responses agent. So content built from bytes either crashes or sends something the service can't read.Description
DataUri.__init__: if nodata_formatis given and the data comes as bytes or an array, default to"base64". A string with no format is still kept as text, and an explicitdata_format(including"plain") is always respected.DataUri.update_data: setting bytes or an array keeps the existing format, or defaults to"base64"if there is none.Behavior change: a data URI built from bytes without a format now looks like
data:image/png;base64,iVBO...rather than the raw text. Before, this either raised an error or, for UTF-8-safe bytes, came out as an unlabeled text payload, so anything that was working gets a valid, self-describing URI.test_send_audiointest_openai_realtime.pyexpectedaudio="audio data", the raw bytes ofb"audio data". It now expects the base64"YXVkaW8gZGF0YQ==", which is what the realtime API takes forinput_audio_buffer.append. Real PCM bytes would have raisedUnicodeDecodeErroron the old path.Tests added:
test_data_uri.py: bytes and array input default to base64 and round-trip throughfrom_data_uri; a string with no format stays text;update_data(bytes)sets base64.test_binary_content.py:BinaryContent/ImageContent/AudioContentwith non-UTF-8 bytes give a;base64,data URI, compare equal, and rebuild from that URI. Also anImageContentfrom bytes survives aChatHistory.to_prompt()→from_rendered_prompt()round trip as anImageContentwith the same bytes.Tested in Docker (python:3.12):
tests/unit/contents,tests/unit/prompt_template,tests/unit/connectors/mcp, and every other unit test file that builds image, audio or binary content (Azure AI Inference, Bedrock, Google AI, Vertex AI, Ollama, OpenAI audio-to-text and realtime, the Azure AI and Responses agents). I left out the ONNX tests because I didn't install that extra.ruff checkandruff format --checkpass with the locked ruff 0.15.5.I made this change with help from Claude Code, and I reviewed the diff and the test results myself.
Contribution Checklist
🤖 Generated with Claude Code