Skip to content

Python: fix(contents): accept RFC 6838 media types and parameters in data URIs - #14663

Open
Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
microsoft:mainfrom
feiiiiii5:semantic-kernel-A-254
Open

Chen Yufeiyang (feiiiiii5) wants to merge 1 commit into
microsoft:mainfrom
feiiiiii5:semantic-kernel-A-254

Conversation

@feiiiiii5

@feiiiiii5 Chen Yufeiyang (feiiiiii5) commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Motivation and Context

Valid RFC 2397 data URIs are rejected or silently mis-decoded when the media type contains + (for example image/svg+xml) or when a parameter precedes base64 (for example data:text/plain;charset=utf-8;base64,...).

ImageContent(data_uri="data:image/svg+xml;base64,PHN2Zy8+") raises ContentInitializationError: Invalid data uri format. DataUri.from_data_uri("data:text/plain;charset=utf-8;base64,SGVsbG8=") does not raise, but returns the base64 text as bytes instead of decoding it, because charset=utf-8;base64 is treated as one data-format string. Both are valid data URIs, and the second is a silent data-corruption path.

Description

The parser now uses RFC 6838/RFC 2045 token characters for the media type and parameters, and keeps the optional data format as a token. This accepts image/svg+xml, vendor types such as application/vnd.ms-excel, and ;charset=utf-8;base64 without treating the parameter as part of the base64 marker.

Tests added for the media-type +, a parameter before base64, a vendor media type, and ImageContent construction from an SVG data URI. On the base commit 483ffb4, the four new tests fail; on this branch the related contents/prompt/template/text suites are 1,111 passed. ruff check and ruff format --check are clean.

Contribution Checklist

  • The code builds clean without any errors or warnings (imports and targeted unit suites).
  • The PR follows the SK contribution guidelines; ruff check and ruff format --check pass on the changed files.
  • All targeted unit tests pass, and new regression tests were added.
  • No breaking change for existing valid data URIs; quoted-string parameters remain out of scope.

The data URI parser only allowed letters and hyphens in the media type
and treated everything after the first semicolon as the data format, so
image/svg+xml was rejected and charset=utf-8;base64 left the base64
payload undecoded. Parse the media type and parameters with the RFC 6838
token character set and keep the optional data format as a token.
@feiiiiii5
Chen Yufeiyang (feiiiiii5) requested a review from a team as a code owner October 26, 2026 06:52
Copilot AI balanced review requested due to automatic review settings October 26, 2026 06:52
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 26, 2026
@github-actions github-actions Bot changed the title fix(contents): accept RFC 6838 media types and parameters in data URIs Python: fix(contents): accept RFC 6838 media types and parameters in data URIs Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The revised parser rejects valid parameter characters and shorthand URIs that omit the media type.

2 open issues
What changed in this PR

This PR updates Python’s data URI parser to accept additional media-type characters and correctly separate parameters from the base64 marker.

Changes:

  • Expands the parser’s media-type and parameter character classes.
  • Adds regression tests for SVG, vendor media types, and charset parameters before base64.
  • Tests ImageContent construction from an SVG data URI.
File Description
python/​tests/​unit/​contents/​test_image_content.py Adds SVG data URI construction coverage.
python/​tests/​unit/​contents/​test_data_uri.py Adds media-type and parameter parsing cases.
python/​semantic_kernel/​contents/​utils/​data_uri.py Revises the data URI parsing expression.

Review effort: Balanced

# the data format as a token prevents unparsed parameters from being silently
# treated as part of the format (for example "charset=utf-8;base64").
pattern = (
r"(((?P<mime_type>[a-zA-Z0-9!#$&^_.+-]+/[a-zA-Z0-9!#$&^_.+-]+)"
# treated as part of the format (for example "charset=utf-8;base64").
pattern = (
r"(((?P<mime_type>[a-zA-Z0-9!#$&^_.+-]+/[a-zA-Z0-9!#$&^_.+-]+)"
r"(?P<parameters>(;[a-zA-Z0-9!#$&^_.+-]+=+[a-zA-Z0-9!#$&^_.+-]+)*))?"

This branch was successfully deployed

1 active deployment
github-app-auth — 6b696f64 Deployed Oct 26, 2026 by feiiiiii5 via add_label #29442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants