Skip to content

[BugFix][Refactor] Modular Transformer Pipeline and Fix Gemini/Anthropic Empty Content Handling #6063

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

Merged
merged 20 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class ModelFamily:
CLAUDE_3_HAIKU = "claude-3-haiku"
CLAUDE_3_SONNET = "claude-3-sonnet"
CLAUDE_3_OPUS = "claude-3-opus"
CLAUDE_3_5_HAIKU = "claude-3.5-haiku"
CLAUDE_3_5_SONNET = "claude-3.5-sonnet"
CLAUDE_3_7_SONNET = "claude-3.7-sonnet"
CLAUDE_3_5_HAIKU = "claude-3-5-haiku"
CLAUDE_3_5_SONNET = "claude-3-5-sonnet"
CLAUDE_3_7_SONNET = "claude-3-7-sonnet"
UNKNOWN = "unknown"

ANY: TypeAlias = Literal[
Expand All @@ -50,8 +50,9 @@ class ModelFamily:
"claude-3-haiku",
"claude-3-sonnet",
"claude-3-opus",
"claude-3.5-haiku",
"claude-3.5-sonnet",
"claude-3-5-haiku",
"claude-3-5-sonnet",
"claude-3-7-sonnet",
"unknown",
]

Expand All @@ -66,6 +67,7 @@ def is_claude(family: str) -> bool:
ModelFamily.CLAUDE_3_OPUS,
ModelFamily.CLAUDE_3_5_HAIKU,
ModelFamily.CLAUDE_3_5_SONNET,
ModelFamily.CLAUDE_3_7_SONNET,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
AsyncGenerator,
Coroutine,
Dict,
Iterable,
List,
Literal,
Mapping,
Expand All @@ -20,6 +21,7 @@
Set,
Union,
cast,
overload,
)

import tiktoken
Expand Down Expand Up @@ -142,20 +144,41 @@
return "image/jpeg"


@overload
def __empty_content_to_whitespace(content: str) -> str: ...


@overload
def __empty_content_to_whitespace(content: List[Any]) -> Iterable[Any]: ...


def __empty_content_to_whitespace(
content: Union[str, List[Union[str, Image]]],
) -> Union[str, Iterable[Any]]:
if isinstance(content, str) and not content.strip():
return " "
elif isinstance(content, list) and not any(isinstance(x, str) and not x.strip() for x in content):
for idx, message in enumerate(content):
if isinstance(message, str) and not message.strip():
content[idx] = " "

Check warning on line 163 in python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py#L158-L163

Added lines #L158 - L163 were not covered by tests

return content

Check warning on line 165 in python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py#L165

Added line #L165 was not covered by tests


def user_message_to_anthropic(message: UserMessage) -> MessageParam:
assert_valid_name(message.source)

if isinstance(message.content, str):
return {
"role": "user",
"content": message.content,
"content": __empty_content_to_whitespace(message.content),
}
else:
blocks: List[Union[TextBlockParam, ImageBlockParam]] = []

for part in message.content:
if isinstance(part, str):
blocks.append(TextBlockParam(type="text", text=part))
blocks.append(TextBlockParam(type="text", text=__empty_content_to_whitespace(part)))

Check warning on line 181 in python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py#L181

Added line #L181 was not covered by tests
elif isinstance(part, Image):
blocks.append(
ImageBlockParam(
Expand All @@ -177,7 +200,7 @@


def system_message_to_anthropic(message: SystemMessage) -> str:
return message.content
return __empty_content_to_whitespace(message.content)

Check warning on line 203 in python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py#L203

Added line #L203 was not covered by tests


def assistant_message_to_anthropic(message: AssistantMessage) -> MessageParam:
Expand All @@ -190,6 +213,7 @@
for func_call in message.content:
# Parse the arguments and convert to dict if it's a JSON string
args = func_call.arguments
args = __empty_content_to_whitespace(args)

Check warning on line 216 in python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py#L216

Added line #L216 was not covered by tests
if isinstance(args, str):
try:
args_dict = json.loads(args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import _message_transform
from ._openai_client import (
AZURE_OPENAI_USER_AGENT,
AzureOpenAIChatCompletionClient,
Expand All @@ -20,4 +21,5 @@
"BaseOpenAIClientConfigurationConfigModel",
"CreateArgumentsConfigModel",
"AZURE_OPENAI_USER_AGENT",
"_message_transform",
]
Loading
Loading