Skip to content

Studio: read PDFs that Claude Code opens through /v1/messages - #18227

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:messages-read-pdf-documents
Nov 18, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:messages-read-pdf-documents

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

When unsloth start claude (or any Messages API client pointed at Studio) reads a PDF with its Read tool, the file comes back as a PDF document inside the tool result. Studio only read plain text documents there, so the model got the file name and a "document omitted" note instead of the contents. It then answered without the document, said it could not read it, or made up the numbers.

PDF documents in tool results on /v1/messages are now turned into text with the same PDF reader web fetch already uses, with the same page and length limits, so the model sees what is in the file. A PDF that cannot be decoded or has no text still gets the omitted note. This extends the document support added in #11152.

PDF documents sent directly in a user turn (not only inside a tool result) are now accepted and read the same way, instead of being refused with a 400. Text is extracted off the event loop before conversion and kept on the request, so a long session with many PDFs is not re-parsed on the event loop, and a small cache keyed by the payload's hash reuses the text across turns without holding the base64.

Screenshots

Before (main) After (this PR)
before after

@NilayYadav NilayYadav changed the title Studio: read PDF files that unsloth start claude opens Studio: read PDFs that Claude Code opens through /v1/messages Nov 17, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T06:09:16.890629Z 8361e66 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f3e37f517e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +99 to +100
@functools.lru_cache(maxsize = 8)
def _pdf_document_text(data: str) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid retaining full PDF payloads as global cache keys

For large PDF tool results, this LRU retains the complete base64 data string for each of eight entries, including failed extractions returning ""; the extracted-text limit does not bound these keys. /v1/messages uses the default 500 MiB request-body cap from utils/upload_limits.py, so eight sequential requests can retain nearly 4 GiB of payloads after the requests finish, potentially exhausting backend RAM. Use digest keys with bounded cached results, or keep extracted text request-local, instead of retaining the source strings globally.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this. The cache is capped at 8 entries, and the PDFs Claude Code sends are bounded by the API request limit and the 20 page Read cap, so retention stays small in practice. Extraction results are unaffected.

Comment on lines +118 to +120
source = item.get("source") if isinstance(item, dict) else None
if (
item.get("type") == "document"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip non-object tool-result entries during PDF warm-up

When tool_result.content contains a non-object entry such as null, AnthropicToolResultBlock accepts it through its bare list type, and the existing converter skips it. The new warmer sets source to None but then calls item.get unconditionally, so both /messages and /messages/count_tokens now raise AttributeError and return HTTP 500, even when the request contains no PDFs. Skip non-dict nested items before inspecting their type, matching the converter's existing behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this. The Messages spec only allows content block objects inside a tool_result content array, so a null entry is malformed input that no real client sends.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f3e37f517e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +45870 to +45872
await asyncio.to_thread(
warm_anthropic_pdf_documents, [m.model_dump() for m in payload.messages]
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Isolate lock-bound PDF work from the shared default executor

When RAG indexing holds MUPDF_LOCK, concurrent requests containing an uncached PDF can occupy every worker in asyncio's default executor while waiting for that lock; lru_cache does not coalesce concurrent misses, so even the same PDF can exhaust the pool. Both Messages handlers now unconditionally submit their warm-up to that executor, including requests without PDFs, so unrelated text-only requests also stall until indexing releases the lock. An isolated reproduction with the host's seven-worker default pool confirmed that a PDF-free warm-up remained queued until lock release. Bound PDF submissions before they occupy shared workers, or dispatch only actual PDF extraction to a dedicated executor while letting PDF-free requests bypass it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this. Filling the executor needs a full pool of uncached PDF requests in flight while indexing holds the lock, and even then the requests just wait and then complete. Nothing is lost or wrong, and the event loop itself stays free.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: f3e37f517e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

A conversation with more than eight PDFs thrashed the LRU, so every conversion
re-parsed them all on the event loop. Cache extracted text by the string's hash
instead of pinning the base64, walk the request models in the worker thread
instead of dumping every message on the loop, and let user turns carry base64
PDF documents now that they are read.

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 8361e667e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit a8cc8bd into unslothai:main Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants