feat: Add PDF document upload support to Anthropic LLM adapter#4727
feat: Add PDF document upload support to Anthropic LLM adapter#4727encounter wants to merge 2 commits intogoogle:mainfrom
Conversation
Adds support for PDF document parts in the Anthropic adapter by converting inline PDF data to Anthropic's `DocumentBlockParam` format using base64 encoding, matching the existing pattern for image part handling.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces the capability to upload PDF documents to the Anthropic LLM adapter. It establishes a mechanism to convert inline PDF data into a structured "DocumentBlockParam" format, similar to how images are handled. The changes also ensure that document parts are appropriately filtered from assistant turns, maintaining consistency with existing content handling policies and providing relevant warnings. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Hello @encounter, thank you for creating this PR! This PR is a new feature, could you please associate a GitHub issue with this PR? If there is no existing issue, could you please create one? This information will help reviewers to review your PR more efficiently. Thanks! Response from ADK Triaging Agent |
There was a problem hiding this comment.
Code Review
This pull request adds support for PDF document uploads to the Anthropic LLM adapter, following the existing implementation for images. The changes are logical and well-tested. I've identified a few areas for improvement, mainly concerning the robustness of a MIME type check and strengthening the test assertions. Overall, this is a good addition.
- Use startswith() for MIME type check to handle parameters (e.g. "application/pdf; name=doc.pdf") - Move import base64 to top of test file per PEP 8 - Parameterize expected warning messages in tests instead of just checking that any warning was logged - Rename test function to test_content_to_message_param for clarity
|
@gemini-code-assist review |
There was a problem hiding this comment.
Code Review
This pull request adds support for PDF document uploads to the Anthropic LLM adapter, following the existing pattern for image uploads. The changes are well-structured and include corresponding unit tests. My review focuses on opportunities to reduce code duplication introduced by mirroring the image handling logic. I've left a couple of comments suggesting refactoring to improve maintainability.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
The Anthropic LLM adapter does not support PDF document uploads. When a user sends a
types.Partwithinline_datacontaining aapplication/pdfMIME type, the adapter raisesNotImplementedErrorbecause there is no handler for document parts.Solution:
Add PDF document support following the same pattern already established for image parts:
_is_document_part()helper to detect PDF inline data, mirroring_is_image_part()DocumentBlockParamwith base64 encoding inpart_to_message_block(), mirroring theImageBlockParamconversioncontent_to_message_param(), consistent with the existing image filtering behaviorTesting Plan
Unit Tests:
Added 3 new test cases:
test_part_to_message_block_with_pdf_document— verifies PDF parts are correctly converted toDocumentBlockParamwith base64 datauser_role_with_text_and_document— verifies documents are included in user turnsmodel_role_with_text_and_document— verifies documents are filtered from assistant turns with a warningAll 30 tests in
test_anthropic_llm.pypass:Manual End-to-End (E2E) Tests:
Tested with an agent that processes PDF resume uploads using the Anthropic adapter. PDF documents are correctly sent to Claude and processed.
Checklist
Additional context
This change is minimal and scoped — it only adds the document handling path alongside the existing image handling path, following the same conventions throughout.