Skip to content

Fix media type matching in unions to use file extension heuristics#2894

Merged
antoniosarosi merged 2 commits intocanaryfrom
fix-media-union-type-matching
Dec 31, 2025
Merged

Fix media type matching in unions to use file extension heuristics#2894
antoniosarosi merged 2 commits intocanaryfrom
fix-media-union-type-matching

Conversation

@antoniosarosi
Copy link
Contributor

Summary

When coercing a value like { file: "document.pdf" } to a union type like (image | pdf), the code previously matched the first variant (image) regardless of the file extension, resulting in invalid MIME types like image/pdf.

This change:

  • Adds extension-based matching for media types in unions (checks .pdf, .png, .mp3, etc.)
  • Handles both { file: "..." } and { url: "..." } forms, including URLs with query params
  • Only applies the heuristic when there are 2+ different media types in the union
  • Falls back to original first-match behavior if no extension match is found
  • Adds strict media type matching for already-coerced BamlValue::Media with fallback for backwards compatibility

Fix #2782

Test plan

  • Existing test with (image | pdf)[] union now correctly identifies PDF files
  • Prompt shows [<application_placeholder base64>] instead of [<image_placeholder base64>]
  • Test passes end-to-end with Gemini

When coercing a value like `{ file: "document.pdf" }` to a union type
like `(image | pdf)`, the code previously matched the first variant
(`image`) regardless of the file extension, resulting in invalid MIME
types like "image/pdf".

This change adds extension-based matching for media types in unions:
- Extract file extension from `{ file: "..." }` or `{ url: "..." }`
- Match against the appropriate media type based on extension
- Fall back to original first-match behavior if no extension match

Also adds strict media type matching when coercing already-coerced
BamlValue::Media to ensure the media_type field matches the target,
with fallback cases for backwards compatibility.
@vercel
Copy link

vercel bot commented Dec 31, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
promptfiddle Skipped Skipped Dec 31, 2025 5:46pm

@antoniosarosi antoniosarosi added this pull request to the merge queue Dec 31, 2025
Merged via the queue into canary with commit cad717c Dec 31, 2025
39 of 42 checks passed
@antoniosarosi antoniosarosi deleted the fix-media-union-type-matching branch December 31, 2025 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] TS: error 400 (cant support image/pdf) when test using pdf in an array

1 participant