Skip to content

Conversation

@sarojrout
Copy link
Contributor

  • Add _is_pdf_part() helper function to detect PDF parts
  • Add PDF handling in part_to_message_block() function
  • PDFs are encoded as base64 and sent as document blocks to Anthropic API
  • Update return type annotation to include DocumentBlockParam for PDF document blocks
  • Add test for PDF support

Fixes #3614

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:

If applicable, please follow the issue templates to provide as much detail as
possible.

Problem:
When using Claude models (e.g., Claude Sonnet 4.5) in ADK with PDF files, the code throws a NotImplementedError: Not supported yet error. The part_to_message_block() function in anthropic_llm.py handles text, images, function calls, and function responses, but does not handle PDF documents. When a user attempts to upload a PDF file (with mime_type="application/pdf"),

Solution:
Added PDF support by:

Creating a _is_pdf_part() helper function (similar to _is_image_part()) to detect PDF parts by checking for mime_type == "application/pdf"
Adding PDF handling in part_to_message_block() function that:
Detects PDF parts using the new helper function
Encodes PDF data as base64 (same as images)
Returns a document block dictionary with type="document" and the base64-encoded PDF data
Updated the return type annotation to include dict[str, Any] for PDF document blocks
Added comprehensive unit test to verify PDF handling works correctly
This solution follows the same pattern used for image handling and leverages Anthropic's API support for PDF documents as document blocks.

Testing Plan

Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.

Unit Tests:

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

Please include a summary of passed pytest results.

Manual End-to-End (E2E) Tests:
Setup:

  1. Configure Claude model with proper Vertex AI credentials
  2. Set environment variables: GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION

Test Steps:

from google.adk import Agent
from google.adk.models.anthropic_llm import Claude

agent = Agent(
    name="pdf_reader",
    model=Claude(model="claude-3-5-sonnet-v2@20241022"),
    instruction="Analyze PDF documents"
)
  1. Upload a PDF file to the agent:
from google.genai import types

# Read PDF file
with open("document.pdf", "rb") as f:
    pdf_data = f.read()

# Create content with PDF
content = types.Content(
    role="user",
    parts=[
        types.Part(
            inline_data=types.Blob(
                mime_type="application/pdf",
                data=pdf_data
            )
        )
    ]
)

# Run agent - should now work without NotImplementedError
async for event in runner.run_async(
    user_id="test-user",
    session_id="test-session",
    new_message=content
):
    print(event)

Expected Result:

  • No NotImplementedError is raised
  • PDF is successfully sent to Claude API as a document block
  • Claude can process and analyze the PDF content
  • Agent responds with analysis of the PDF

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. (Note: Manual testing requires Vertex AI setup with Claude access)
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Code Changes Summary:

File: src/google/adk/models/anthropic_llm.py
Added _is_pdf_part() helper function (lines 79-85)
Added PDF handling in part_to_message_block() (lines 147-159)
Updated return type annotation (line 95)
File: tests/unittests/models/test_anthropic_llm.py
Added test_part_to_message_block_with_pdf() test (lines 467-496)

Technical Details:

PDFs are handled similarly to images: base64-encoded and sent as document blocks
The implementation follows Anthropic's API specification for document blocks
The fix is backward compatible - existing functionality (text, images, function calls) remains unchanged

- Add _is_pdf_part() helper function to detect PDF parts
- Add PDF handling in part_to_message_block() function
- PDFs are encoded as base64 and sent as document blocks to Anthropic API
- Update return type annotation to include DocumentBlockParam for PDF document blocks
- Add test for PDF support

Fixes google#3614
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @sarojrout, 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 resolves a NotImplementedError encountered when attempting to use Claude models with PDF files within the ADK. It introduces robust support for PDF document handling by enabling the system to detect, base64 encode, and correctly format PDF data as DocumentBlockParam types for the Anthropic API. This enhancement ensures that PDF inputs are processed similarly to images, expanding the multimodal capabilities of the Claude integration.

Highlights

  • PDF Part Detection: A new helper function, _is_pdf_part(), has been added to accurately detect parts containing PDF data by checking for the 'application/pdf' mime type.
  • PDF Handling in Message Blocks: The part_to_message_block() function now includes logic to handle PDF documents. PDFs are base64 encoded and structured as DocumentBlockParam types, which are then sent to the Anthropic API.
  • Type Annotation Update: The return type annotation for part_to_message_block() has been updated to include anthropic_types.DocumentBlockParam, reflecting the new support for PDF document blocks.
  • Unit Test Coverage: A comprehensive unit test, test_part_to_message_block_with_pdf(), has been added to verify the correct handling and encoding of PDF data.
  • Refactoring and Simplification: The AnthropicLlm class has been renamed to Claude, and its internal client management has been streamlined to directly use AnthropicVertex.
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 Dec 4, 2025
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 refactors the Anthropic LLM integration by renaming the AnthropicLlm class to Claude and updating its default model to claude-3-5-sonnet-v2@20241022. The changes include migrating from AsyncAnthropic and AsyncAnthropicVertex clients to the synchronous AnthropicVertex client, which involved adjusting the messages.create call to remove the await keyword. A new feature was added to support PDF documents, introducing a helper function _is_pdf_part and modifying part_to_message_block to convert PDF types.Part objects into anthropic_types.DocumentBlockParam using base64 encoding. Correspondingly, the AnthropicLlm class and its specific test case were removed, and a new test was added to validate the PDF handling.

@ryanaiagent ryanaiagent self-assigned this Dec 4, 2025
@ryanaiagent ryanaiagent added the needs-review [Status] The PR is awaiting review from the maintainer label Dec 4, 2025
@ryanaiagent
Copy link
Collaborator

Hi @sarojrout ,Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

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 needs-review [Status] The PR is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude imported from anthropic_llm throws NotImplementedError when using Anthropic LLM while uploading PDFs

3 participants