Fix base64 deserialization when JSON encoder escapes forward slashes#1342
Open
Fix base64 deserialization when JSON encoder escapes forward slashes#1342
Conversation
…k, AudioContentBlock, and BlobResourceContents The ContentBlock.Converter and ResourceContents.Converter were using reader.ValueSpan.ToArray() to read base64-encoded data from JSON strings. This reads raw bytes without unescaping JSON escape sequences. When base64 data contains '/' characters that are JSON-escaped as '\/' (a valid JSON escape used by some encoders), the backslash corrupts the base64 data, causing "Invalid base64 data" FormatException on access to DecodedData. Fix: Check reader.ValueIsEscaped and fall back to GetString() + UTF8 encoding when escape sequences are present, preserving the fast path for the common unescaped case. Fixes #1340 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
stephentoub
reviewed
Feb 21, 2026
| } | ||
| else | ||
| { | ||
| data = Encoding.UTF8.GetBytes(reader.GetString()!); |
Contributor
There was a problem hiding this comment.
@ericstj seems like this will be the majority case, such that the switch in representation is actually going to make these cases worse?
Copilot
AI
changed the title
[WIP] Fix invalid base64 error in ImageContentBlock
Fix base64 deserialization when JSON encoder escapes forward slashes
Feb 21, 2026
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.
ImageContentBlock,AudioContentBlock, andBlobResourceContentsfail withFormatException: Invalid base64 datawhen the upstream JSON encoder escapes/as\/in base64 strings — a valid JSON escape that many encoders produce.The custom converters used
reader.ValueSpan.ToArray()which returns raw unescaped bytes. The literal\from\/escapes is not a valid base64 character, soBase64.DecodeFromUtf8rejects it.Changes
ContentBlock.Converter.ReadandResourceContents.Converter.Read: Checkreader.ValueIsEscaped; when true, fall back toreader.GetString()+Encoding.UTF8.GetBytes()to get properly unescaped UTF-8 bytes. The fastValueSpanpath is preserved for the common case./.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.