Repository navigation
Python: [Feature]: Single shared data-URI parser and media-type sniffer used by all packages #8913
Copy link
Copy link
Closed
Closed
Feature
Copy link
Labels
agentsUsage: [Issues, PRs], Target: Single agentUsage: [Issues, PRs], Target: Single agentmcpUsage: [Issues, PRs], Target: MCPUsage: [Issues, PRs], Target: MCPpythonUsage: [Issues, PRs], Target: PythonUsage: [Issues, PRs], Target: PythonskillsUsage: [Issues, PRs], Target: skills related featuresUsage: [Issues, PRs], Target: skills related features
Description
Description
Problem
Data-URI parsing and media-type detection are implemented several times, and each copy has been fixed separately:
- A2A: Python: Python A2A rejects data URIs with media type parameters #6820 / Python: accept A2A data URI media parameters #6818 (media-type parameters)
- AG-UI: Python: AG-UI state extraction ignores parameterized JSON data URIs #6902 / Python: accept AG-UI state data URI parameters #6905 (parameterized JSON data URIs)
- Gemini: Python: Fix GeminiChatClient dropping image/file content #6751; malformed URI: Python: malformed data_uri should not leak IndexError #6917; Python: reject non-base64 data URIs in detect_media_type_from_base64 #6629 (closed, non-base64 URIs)
- Core is still affected: Python: [Bug]: Content.from_uri rejects valid non-base64 data URIs with parameters and drops the media type #8905 (
_validate_urirejectsdata:text/plain;charset=utf-8,...and drops the media type) and Python: [Bug]: detect_media_type_from_base64 misclassifies BMP/SVG and misses MPEG-2 audio #8906 (loose BMP/SVG sniffing, missed MPEG-2 audio, non-validated base64).
Proposal
- Add one internal helper in core, e.g.
parse_data_uri(uri) -> DataUri(media_type, params, is_base64, data). It follows RFC 2397: default media typetext/plain;charset=US-ASCII, arbitrarykey=valueparams, base64 only when the last param isbase64, percent-decoding for non-base64, and a clearValueError/ContentErroron malformed input. - Add one sniffer with strict signatures: BMP DIB-header check, SVG requiring an
<svgroot, the MPEG frame-sync mask, WebP/WAV RIFF subtype, andb64decode(validate=True). - Replace the ad-hoc
data:handling found in about 15 modules (A2A, hosting-A2A, AG-UI, Gemini, OpenAI Responses/Chat Completions, core MCP, hosting-MCP, DevUI, Foundry hosting, Content Understanding, skills) with the shared helper. - Add a shared table-driven test corpus (valid and invalid URIs, magic-byte samples) run once in core.
Expected behavior
Every package accepts and rejects exactly the same data URIs and reports the same media type.
Code Sample
from agent_framework._types import parse_data_uri
u = parse_data_uri("data:text/plain;charset=utf-8,hello%20world")
u.media_type # "text/plain"
u.params # {"charset": "utf-8"}
u.data # b"hello world"Language/SDK
Python
Reactions are currently unavailable
Metadata
Metadata
Labels
agentsUsage: [Issues, PRs], Target: Single agentUsage: [Issues, PRs], Target: Single agentmcpUsage: [Issues, PRs], Target: MCPUsage: [Issues, PRs], Target: MCPpythonUsage: [Issues, PRs], Target: PythonUsage: [Issues, PRs], Target: PythonskillsUsage: [Issues, PRs], Target: skills related featuresUsage: [Issues, PRs], Target: skills related features
Type
Projects
- StatusShow more project fieldsDone
Activity
eavanvalkenburg commentedon Oct 27, 2026
Thanks for laying out the repeated parsing problems. We will consider shared internal data-URI parsing infrastructure in core MAF, reusing the existing parser and media-type detector rather than adding parallel helpers. That does not commit us to identical input acceptance across providers: each integration still needs to enforce its own supported formats and transport contract. The immediate core fixes from #8918 are already on main; the broader consolidation should be planned alongside the scoped percent-encoding work in #9653 / #9664. This is consideration of the direction, not a commitment to implementation or timing.
eavanvalkenburg commentedon Oct 27, 2026
/plan Scope this to the smallest safe consolidation of existing internal data-URI parsing primitives, not a second media sniffer or a promise that every provider accepts identical input. Inventory current core _parse_data_uri / Content accessors / detect_media_type_from_base64 and the actual duplicate parsing consumers; separate common RFC 2397 syntax, parameters/default media type, percent encoding, base64 validation and byte extraction from integration-specific format/transport restrictions. Identify which reported parsing/sniffing defects #8918 and subsequent merged fixes already address. Coordinate with the current #9653 planning and #9664 implementation/review so percent-encoded base64, lazy validation versus actual transport decoding, and provider parity are handled once rather than duplicated. Preserve raw/encoded payload behavior, media-type overrides, malformed-input errors, logging without payload disclosure, and adapter-specific JSON/object or provider constraints. Propose a bounded migration sequence with explicit dependency/API/compatibility costs; prefer reusing existing helpers without adding public API unless demonstrated necessary. Include shared valid/invalid URI corpus and focused adapter regressions, not only core tests. Existing assignment is preserved; this planning request does not mark the issue implementation-ready or authorize an all-packages refactor.