Repository: phidatahq/phidata
File: libs/agno/agno/utils/models/claude.py (Python)

Code before:
        return None


def _format_file_for_message(file: File) -> Optional[Dict[str, Any]]:
    """
    Add a document url or base64 encoded content to a message.
    """

    mime_mapping: dict[str, str] = {
        "application/pdf": "base64",
    }
    # All text/* MIME types use the "text" source type
    for _mt in (
        "text/plain",
        "text/csv",
        "text/html",
        "text/css",
        "text/md",
        "text/xml",
        "text/rtf",
        "text/javascript",
        "text/x-python",
        "application/json",
        "application/x-javascript",
        "application/x-python",
    ):
        mime_mapping[_mt] = "text"

    # Case 0: File is an Anthropic uploaded file
    if file.external is not None and hasattr(file.external, "id"):
        return {
            "type": "document",
            "source": {
                "type": "file",
                "file_id": file.external.id,
            },
        }

    # Case 1: Document is a URL
    if file.url is not None:
        return {
            "type": "document",
            "source": {
                "type": "url",
                "url": file.url,
            },
            "citations": {"enabled": True},
        }
    # Case 2: Document is a local file path
    elif file.filepath is not None:
        import base64

Review comment (suggestion):
The Anthropic API only accepts two document source types: `PlainTextSourceParam` (type="text", media_type must be literal "text/plain") and `Base64PDFSourceParam` (type="base64", media_type must be literal "application/pdf"). This mapping routes each MIME type to the correct source type.

Code after:
        return None


def _format_file_for_message(file: File) -> Optional[Dict[str, Any]]:
    """
    Add a document url or base64 encoded content to a message.
    """

    mime_mapping: dict[str, str] = {
        "application/pdf": "base64",
        # All text/* MIME types use the "text" source type
        "text/plain": "text",
        "text/csv": "text",
        "text/html": "text",
        "text/css": "text",
        "text/md": "text",
        "text/xml": "text",
        "text/rtf": "text",
        "text/javascript": "text",
        "text/x-python": "text",
        "application/json": "text",
        "application/x-javascript": "text",
        "application/x-python": "text",
    }

    # Case 0: File is an Anthropic uploaded file
    if file.external is not None and hasattr(file.external, "id"):
        return {
            "type": "document",
            "source": {
                "type": "file",
                "file_id": file.external.id,
            },
        }

    # Case 1: Document is a URL
    if file.url is not None:
        return {
            "type": "document",
            "source": {
                "type": "url",
                "url": file.url,
            },
            "citations": {"enabled": True},
        }
    # Case 2: Document is a local file path
    elif file.filepath is not None:
        import base64
        from pathlib import Path

        path = Path(file.filepath) if isinstance(file.filepath, str) else file.filepath