Skip to content
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

fix: Normalize openai client stop reason to make more robust #5027

Merged
merged 4 commits into from
Jan 13, 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 @@ -52,7 +52,7 @@ class RequestUsage:
completion_tokens: int


FinishReasons = Literal["stop", "length", "function_calls", "content_filter"]
FinishReasons = Literal["stop", "length", "function_calls", "content_filter", "unknown"]


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Image,
MessageHandlerContext,
)
from autogen_core.models import FinishReasons
from autogen_core.logging import LLMCallEvent
from autogen_core.models import (
AssistantMessage,
Expand Down Expand Up @@ -327,6 +328,21 @@
return name


def normalize_stop_reason(stop_reason: str | None) -> FinishReasons:
if stop_reason is None:
return "unknown"

Check warning on line 333 in python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py#L333

Added line #L333 was not covered by tests

# Convert to lower case
stop_reason = stop_reason.lower()

KNOWN_STOP_MAPPINGS: Dict[str, FinishReasons] = {
"end_turn": "stop",
"tool_calls": "function_calls",
}

return KNOWN_STOP_MAPPINGS.get(stop_reason, "unknown")


class BaseOpenAIChatCompletionClient(ChatCompletionClient):
def __init__(
self,
Expand Down Expand Up @@ -747,8 +763,8 @@
else:
prompt_tokens = 0

if stop_reason is None:
raise ValueError("No stop reason found")
if stop_reason == "function_call":
raise ValueError("Function calls are not supported in this context")

Check warning on line 767 in python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py#L767

Added line #L767 was not covered by tests

content: Union[str, List[FunctionCall]]
if len(content_deltas) > 1:
Expand All @@ -770,13 +786,9 @@
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
)
if stop_reason == "function_call":
raise ValueError("Function calls are not supported in this context")
if stop_reason == "tool_calls":
stop_reason = "function_calls"

result = CreateResult(
finish_reason=stop_reason, # type: ignore
finish_reason=normalize_stop_reason(stop_reason),
content=content,
usage=usage,
cached=False,
Expand Down
Loading