Skip to content

feat: Add PDF document upload support to Anthropic LLM adapter#4727

Open
encounter wants to merge 2 commits intogoogle:mainfrom
encounter:feat/anthropic-document-upload
Open

feat: Add PDF document upload support to Anthropic LLM adapter#4727
encounter wants to merge 2 commits intogoogle:mainfrom
encounter:feat/anthropic-document-upload

Conversation

@encounter
Copy link

@encounter encounter commented Mar 5, 2026

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.Part with inline_data containing a application/pdf MIME type, the adapter raises NotImplementedError because there is no handler for document parts.

Solution:
Add PDF document support following the same pattern already established for image parts:

  • Add _is_document_part() helper to detect PDF inline data, mirroring _is_image_part()
  • Convert PDF parts to Anthropic's DocumentBlockParam with base64 encoding in part_to_message_block(), mirroring the ImageBlockParam conversion
  • Filter document parts from assistant turns with a warning in content_to_message_param(), consistent with the existing image filtering behavior

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added 3 new test cases:

  • test_part_to_message_block_with_pdf_document — verifies PDF parts are correctly converted to DocumentBlockParam with base64 data
  • user_role_with_text_and_document — verifies documents are included in user turns
  • model_role_with_text_and_document — verifies documents are filtered from assistant turns with a warning

All 30 tests in test_anthropic_llm.py pass:

tests/unittests/models/test_anthropic_llm.py  30 passed in 3.34s

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

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

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.

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.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • PDF Document Upload: Enabled support for uploading PDF documents to the Anthropic LLM adapter, converting them into Anthropic's "DocumentBlockParam" format using base64 encoding.
  • Consistent Handling: Implemented PDF handling following the established pattern for image parts, ensuring consistency in how different media types are processed.
  • Assistant Turn Filtering: Introduced filtering for document parts in assistant turns, logging a warning, which aligns with the existing behavior for image filtering.

🧠 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
  • src/google/adk/models/anthropic_llm.py
    • Added a new helper function _is_document_part to identify PDF content based on its MIME type.
    • Extended part_to_message_block to process application/pdf inline data, encoding it in base64 and wrapping it in an anthropic_types.DocumentBlockParam.
    • Modified content_to_message_param to include a check for document parts in non-user roles, logging a warning and skipping the part if found, mirroring image handling.
  • tests/unittests/models/test_anthropic_llm.py
    • Introduced test_part_to_message_block_with_pdf_document to verify the correct conversion of PDF data into the expected Anthropic document block structure.
    • Expanded content_to_message_param_test_cases with two new scenarios: one for a user role with text and a document (expecting both to be included) and another for a model role with text and a document (expecting the document to be filtered with a warning).
    • Updated the assertion in test_content_to_message_param_with_images to generally check for mock_logger.warning.assert_called_once() instead of a specific message, accommodating new warning types.
Activity
  • Added a new unit test, test_part_to_message_block_with_pdf_document, to confirm that PDF parts are correctly converted into the DocumentBlockParam format.
  • Included a test case, user_role_with_text_and_document, to verify that documents are properly included when present in user turns.
  • Implemented a test case, model_role_with_text_and_document, to ensure that documents are filtered from assistant turns and a warning is logged, aligning with expected behavior.
  • Confirmed that all 30 existing tests in test_anthropic_llm.py continue to pass after these changes.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the models [Component] Issues related to model support label Mar 5, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Mar 5, 2026

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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
@encounter
Copy link
Author

@gemini-code-assist review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic LLM adapter does not support PDF document uploads

2 participants