Skip to content

feat(anthropic): Add image url support to anthropic #7961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

meng-tsai
Copy link

This PR updates the langchain-anthropic package to support image inputs provided as URLs, in addition to the existing base64 encoding support.

According to the latest Anthropic documentation, Claude 3 models can accept image inputs via a URL source: {"type": "image", "source": {"type": "url", "url": "..."}}.

This change modifies the _formatImage utility function in libs/langchain-anthropic/src/utils/message_inputs.ts to detect if the input string is a valid URL. If it is, it formats the message payload accordingly; otherwise, it falls back to the base64 parsing logic.

This resolves the limitation where LangchainJS previously only accepted base64-encoded images, allowing users to leverage the full capabilities of the Anthropic API for multimodal inputs more easily.

Test Suites: 1 skipped, 56 passed, 56 of 57 total
Tests:       12 skipped, 323 passed, 335 total
Snapshots:   39 passed, 39 total
Time:        5.396 s
Ran all test suites.

Copy link

vercel bot commented Apr 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Apr 3, 2025 11:28pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Apr 3, 2025 11:28pm

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. auto:improvement Medium size change to existing code to handle new use-cases labels Apr 3, 2025
media_type: match[1] ?? "",
data: match[2] ?? "",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this cast?

Copy link
Author

Choose a reason for hiding this comment

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

I'm just following the cast in the previous implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants