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 11 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 @@ -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 @@ def get_mime_type_from_image(image: Image) -> Literal["image/jpeg", "image/png",
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] = " "

return content


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)))
elif isinstance(part, Image):
blocks.append(
ImageBlockParam(
Expand All @@ -177,7 +200,7 @@ def user_message_to_anthropic(message: UserMessage) -> MessageParam:


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


def assistant_message_to_anthropic(message: AssistantMessage) -> MessageParam:
Expand All @@ -190,6 +213,7 @@ def assistant_message_to_anthropic(message: AssistantMessage) -> MessageParam:
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)
if isinstance(args, str):
try:
args_dict = json.loads(args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from . import _message_transform
from ._openai_client import (
AZURE_OPENAI_USER_AGENT,
AzureOpenAIChatCompletionClient,
BaseOpenAIChatCompletionClient,
OpenAIChatCompletionClient,
AZURE_OPENAI_USER_AGENT,
)
from .config import (
AzureOpenAIClientConfigurationConfigModel,
Expand All @@ -20,4 +21,5 @@
"BaseOpenAIClientConfigurationConfigModel",
"CreateArgumentsConfigModel",
"AZURE_OPENAI_USER_AGENT",
"_message_transform",
]
Loading