#10659·agno

[Bug] Claude document formatting labels unguessable files (e.g. .md) as application/pdf

Author: ch-z-hcCreated Sep 29, 2026Updated Sep 29, 2026

Description and Issues

agno/utils/models/claude.py → _format_file_for_message(), local-filepath branch:

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

When File.mime_type is not set and mimetypes cannot guess the type, the file is declared a PDF and base64-encoded. Python's mimetypes table has no entry for .md, .markdown, .rst or .log (verified on 3.11.9: all return None), so attaching a Markdown document to an Anthropic message sends {"type": "base64", "media_type": "application/pdf"} with markdown text inside.

Anthropic's own SDK types are the constraint that makes this a hard failure: Base64PDFSourceParam.media_type is Literal["application/pdf"] and the text source is Literal["text/plain"] — there is no generic binary document source. So the request either errors out or the model is handed a "PDF" that is not a PDF.

A second, platform-dependent face of the same line: mimetypes is registry-driven on Windows, where guess_type("report.csv") answers application/vnd.ms-excel. That is not a key in mime_mapping, so a .csv with no explicit mime_type goes down the base64 branch too. Your own test tests/unit/utils/test_claude.py::TestFormatFileForMessage::test_filepath_no_mime_guesses_from_extension asserts it must resolve to the text source — it passes on Linux CI and fails on Windows, so the intended behaviour is already written down, it is just not what the code guarantees cross-platform.

Steps to Reproduce

Click to expand
No API key or model call needed — this is the payload the formatter produces.

```bash
git clone https://github.com/agno-agi/agno && cd agno/libs/agno
pip install -e . && pip install anthropic
python
import tempfile
from pathlib import Path
from agno.media import File
from agno.utils.models.claude import _format_file_for_message

d = Path(tempfile.mkdtemp())
for name, body in [("notes.md", b"# Title\nbody"), ("report.csv", b"a,b\n1,2")]:
    p = d / name
    p.write_bytes(body)
    src = _format_file_for_message(File(filepath=str(p)))["source"]
    print(f"{name:11} -> type={src['type']:7} media_type={src['media_type']}")

Actual (Windows, Python 3.11):

notes.md    -> type=base64  media_type=application/pdf
report.csv  -> type=base64  media_type=application/vnd.ms-excel

Expected: both should use the text source with media_type="text/plain", which is the branch _format_file_for_message already has and which the mime_mapping docstring comment describes as the destination for all text content.

Also observable without any local file: on this interpreter mimetypes.guess_type("x.md")[0], x.markdown, x.rst, x.log all return None.


</details>

## Expected Behavior

<details id="expected-behaviour" open>
<summary>Click to expand</summary>

```text
Media type for a document with no explicit `File.mime_type` should be resolved from what the file actually is, not assumed:

- extensions that are always text (`.csv`, `.tsv`, `.md`, `.markdown`, `.rst`, `.log`) route to the text source regardless of whether the platform's `mimetypes` knows them
- if nothing can be guessed, fall back to `application/pdf` only when the bytes start with `%PDF-`, otherwise `text/plain`

An explicitly provided `File.mime_type` should keep winning, and behaviour for every media type `mimetypes` already resolves correctly should be unchanged.

Relevant Package Output

Click to expand
Python 3.11.9 on Windows 11, agno from `main` (shallow clone, 2026-09-29), anthropic SDK current.

pytest tests/unit/utils/test_claude.py:
  test_filepath_no_mime_guesses_from_extension ....... FAILED (assert 'base64' == 'text')

mimetypes probes on the same interpreter:
  x.md -> None          x.markdown -> None      x.rst -> None       x.log -> None
  x.tsv -> text/tab-separated-values            x.csv -> application/vnd.ms-excel
  '.md' in mimetypes.types_map -> False

Anthropic SDK type literals (site-packages/anthropic/types):
  base64_pdf_source_param.py   media_type: Required[Literal["application/pdf"]]
  plain_text_source_param.py   media_type: Required[Literal["text/plain"]]

Environment: agno 2.x from main, Windows 11, no API keys involved.

Additional Context

Identical or "application/pdf" fallbacks exist in agno/utils/openai.py (3 call sites) and agno/models/google/gemini.py (4 call sites). Those providers do accept arbitrary inline media types, so they are not broken the same way — flagging only so the pattern is not copied further. A separate small gap on this path: mime_mapping lists text/javascript but modern mimetypes answers application/javascript for .js, which also falls through to base64.

I have a fix with three regression tests ready and will open a PR referencing this issue.