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

Code before:

    # 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
        if path.exists() and path.is_file():
            raw_bytes = path.read_bytes()

            # Determine media type
            media_type = file.mime_type
            if media_type is None:
                import mimetypes

                media_type = mimetypes.guess_type(file.filepath)[0] or "application/pdf"

            source_type = mime_mapping.get(media_type, "base64")

            if source_type == "text":
                return {
                    "type": "document",
                    "source": {
                        "type": "text",
                        "media_type": "text/plain",
                        "data": raw_bytes.decode("utf-8", errors="replace"),
                    },
                    "citations": {"enabled": True},
                }
            else:
                return {

Review comment (suggestion):
Previously this was `base64.standard_b64encode(path.read_bytes())` — encoding happened BEFORE checking the MIME type. So even text/plain files got base64-encoded gibberish sent as "text" source data. Now we read raw bytes first and encode conditionally.

Code after:
        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
        if path.exists() and path.is_file():
            raw_bytes = path.read_bytes()

            # Determine media type
            media_type = file.mime_type
            if media_type is None:
                import mimetypes

                media_type = mimetypes.guess_type(file.filepath)[0] or "application/pdf"

            # Map media type to source type, default to "base64" if no mapping exists
            source_type = mime_mapping.get(media_type, "base64")

            if source_type == "text":
                return {
                    "type": "document",
                    "source": {
                        "type": "text",
                        "media_type": "text/plain",
                        "data": raw_bytes.decode("utf-8", errors="replace"),
                    },
                    "citations": {"enabled": True},
                }
            else:
                return {
                    "type": "document",
                    "source": {