From bca4d7e82f9846df2e0739e5afad33b04b5ec0b9 Mon Sep 17 00:00:00 2001 From: EeS Date: Sat, 22 Mar 2025 16:46:55 +0900 Subject: [PATCH] FIX: Anthropic multimodal(Image) message for Anthropic >= 0.48 aware (#6054) ## Why are these changes needed? This PR fixes a `TypeError: Cannot instantiate typing.Union` that occurs when using the `MultimodalWebSurfer_agent` with Anthropic models. The error was caused by the incorrect usage of `typing.Union` as a class constructor instead of a type hint within the `_anthropic_client.py` file. The code was attempting to instantiate `typing.Union`, which is not allowed. The fix correctly uses `typing.Union` within type hints, and uses the correct `Base64ImageSourceParam` type. It also updates the `pyproject.toml` dependency. ## Related issue number Closes #6035 ## Checks - [ ] I've included any doc changes needed for . See to build and test documentation locally. - [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [v] I've made sure all auto checks have passed. --------- Co-authored-by: Victor Dibia --- python/packages/autogen-ext/pyproject.toml | 1 + .../autogen_ext/models/anthropic/_anthropic_client.py | 4 ++-- python/uv.lock | 10 +++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/python/packages/autogen-ext/pyproject.toml b/python/packages/autogen-ext/pyproject.toml index ec37f3743..93315d45d 100644 --- a/python/packages/autogen-ext/pyproject.toml +++ b/python/packages/autogen-ext/pyproject.toml @@ -19,6 +19,7 @@ dependencies = [ ] [project.optional-dependencies] +anthropic = ["anthropic>=0.48"] langchain = ["langchain_core~= 0.3.3"] azure = [ "azure-ai-inference>=1.0.0b7", diff --git a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py index f0d03cc33..5788f1bf4 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py @@ -25,6 +25,7 @@ from typing import ( import tiktoken from anthropic import AsyncAnthropic, AsyncStream from anthropic.types import ( + Base64ImageSourceParam, ContentBlock, ImageBlockParam, Message, @@ -36,7 +37,6 @@ from anthropic.types import ( ToolResultBlockParam, ToolUseBlock, ) -from anthropic.types.image_block_param import Source from autogen_core import ( EVENT_LOGGER_NAME, TRACE_LOGGER_NAME, @@ -160,7 +160,7 @@ def user_message_to_anthropic(message: UserMessage) -> MessageParam: blocks.append( ImageBlockParam( type="image", - source=Source( + source=Base64ImageSourceParam( type="base64", media_type=get_mime_type_from_image(part), data=part.to_base64(), diff --git a/python/uv.lock b/python/uv.lock index 19024c2f2..8e0688210 100644 --- a/python/uv.lock +++ b/python/uv.lock @@ -290,7 +290,7 @@ wheels = [ [[package]] name = "anthropic" -version = "0.45.2" +version = "0.49.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, @@ -301,9 +301,9 @@ dependencies = [ { name = "sniffio" }, { name = "typing-extensions" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/15/74/2b2485fc120da834c0c5be07462541ec082e9fa8851d845f2587e480535a/anthropic-0.45.2.tar.gz", hash = "sha256:32a18b9ecd12c91b2be4cae6ca2ab46a06937b5aa01b21308d97a6d29794fb5e", size = 200901 } +sdist = { url = "https://files.pythonhosted.org/packages/86/e3/a88c8494ce4d1a88252b9e053607e885f9b14d0a32273d47b727cbee4228/anthropic-0.49.0.tar.gz", hash = "sha256:c09e885b0f674b9119b4f296d8508907f6cff0009bc20d5cf6b35936c40b4398", size = 210016 } wheels = [ - { url = "https://files.pythonhosted.org/packages/74/86/e81814e542d1eaeec84d2312bec93a99b9ef1d78d9bfae1fc5dd74abdf15/anthropic-0.45.2-py3-none-any.whl", hash = "sha256:ecd746f7274451dfcb7e1180571ead624c7e1195d1d46cb7c70143d2aedb4d35", size = 222797 }, + { url = "https://files.pythonhosted.org/packages/76/74/5d90ad14d55fbe3f9c474fdcb6e34b4bed99e3be8efac98734a5ddce88c1/anthropic-0.49.0-py3-none-any.whl", hash = "sha256:bbc17ad4e7094988d2fa86b87753ded8dce12498f4b85fe5810f208f454a8375", size = 243368 }, ] [[package]] @@ -589,6 +589,9 @@ dependencies = [ ] [package.optional-dependencies] +anthropic = [ + { name = "anthropic" }, +] azure = [ { name = "azure-ai-inference" }, { name = "azure-core" }, @@ -719,6 +722,7 @@ dev = [ [package.metadata] requires-dist = [ { name = "aiofiles", marker = "extra == 'openai'" }, + { name = "anthropic", marker = "extra == 'anthropic'", specifier = ">=0.48" }, { name = "asyncio-atexit", marker = "extra == 'docker'", specifier = ">=1.0.1" }, { name = "autogen-agentchat", marker = "extra == 'file-surfer'", editable = "packages/autogen-agentchat" }, { name = "autogen-agentchat", marker = "extra == 'magentic-one'", editable = "packages/autogen-agentchat" },