Fix #5930: Extract text from PDFs in ReadFileTool instead of returning base64#5932
Open
devin-ai-integration[bot] wants to merge 1 commit into
Open
Fix #5930: Extract text from PDFs in ReadFileTool instead of returning base64#5932devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
…g base64 When using input_files with PDFFile, the read_file tool was returning the entire PDF as base64-encoded binary data. This caused: - Massive context bloat for the LLM - Inconsistent responses and context overflow - The same file being re-processed on each tool call Now ReadFileTool detects application/pdf content and extracts text using pypdf (already a dependency via crewai-files) instead of base64-encoding the raw bytes. Each page is labeled with a page number header for clarity. Graceful fallbacks are provided when: - pypdf is not installed (short install message) - The PDF contains no extractable text (friendly message) - The PDF is corrupted (error message, never base64) Co-Authored-By: João <[email protected]>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5930 — When using
input_fileswithPDFFile, theread_filetool was base64-encoding the entire raw PDF binary and injecting it into the LLM context. This caused massive context bloat, inconsistent responses, and context overflow for even medium-sized documents.Root cause:
ReadFileTool._run()checked if the content type was a text type, and for anything else (includingapplication/pdf) it fell through to a base64 encoding path.Fix: Added PDF-specific handling in
ReadFileTool._run()that usespypdf(already a dependency viacrewai-files) to extract structured text from PDF pages. Each page is labeled with a--- Page N ---header. Graceful fallbacks are provided for:The extracted text is dramatically smaller than base64-encoded binary, keeping the LLM context manageable.
Review & Testing Checklist for Human
ReadFileTool._run()correctly extracts text from a real multi-page PDF (not just the synthetic test PDFs)input_files={"doc": PDFFile(source="path/to/real.pdf")}and verify the agent receives readable text, not base64Notes
pypdfis already a dependency ofcrewai-files, so no new dependencies are introducedread_filetool path — files that are auto-injected via multimodal LLM support (e.g., images on GPT-4o) are unaffectedLink to Devin session: https://app.devin.ai/sessions/0922c01f2b064a7e8e0a6a2f05e9ab09