Skip to content

Conversation

@zhcn000000
Copy link
Contributor

@zhcn000000 zhcn000000 commented Dec 21, 2025

Improve the way of selecting file extension or mediatype and support automatic selection of file mediatype for FileUrl or BinaryContent
Fix #3349

Copilot AI review requested due to automatic review settings December 21, 2025 11:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request improves the file extension and media type selection mechanism for FileUrl and BinaryContent classes. The key changes enable automatic media type detection using Magika (preferred) or python-magic (fallback) when media type is not explicitly provided.

Key Changes:

  • Implements automatic media type detection for BinaryContent using Magika or python-magic libraries
  • Refactors FileUrl subclasses to use a common _infer_media_type base implementation with fallback to mimetypes.guess_type
  • Adds internal caching fields (_media_type, _type, _extension) to optimize repeated access
  • Updates error messages to be more generic (e.g., "Could not infer media type from URL" instead of media-specific messages)

Reviewed changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
pydantic_ai_slim/pydantic_ai/messages.py Core implementation of automatic media type detection, adds computed property for media_type, refactors FileUrl format inference, and adds caching fields
pydantic_ai_slim/pyproject.toml Adds python-magic>=0.4.27 as required dependency and magika>=1.0.1 as optional dependency
tests/test_messages.py Updates test snapshots to include _media_type parameter and removes obsolete test for DocumentUrl format error
tests/test_vercel_ai.py Updates test snapshots to include _media_type parameter in BinaryImage and BinaryContent instances
tests/test_mcp.py Updates test snapshots to include _media_type parameter
tests/test_fastmcp.py Updates test snapshots to include _media_type parameter
tests/test_agent_output_schemas.py Updates JSON schema snapshots to reflect new computed field behavior and reordered required fields
tests/test_agent.py Updates test snapshots to include _media_type parameter
tests/models/test_openai_responses.py Updates test snapshots across multiple test cases to include _media_type parameter
tests/models/test_mistral.py Updates test snapshots to include _media_type parameter
tests/models/test_google.py Updates test snapshots to include _media_type parameter
tests/evals/test_reporting.py Updates table formatting snapshots (unrelated formatting changes)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Add multimodel custom fileextension support

1 participant