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

finish_reason of non-openai models not handled in OpenAIChatCompletionClient #5020

Closed
ravishqureshi opened this issue Jan 13, 2025 · 2 comments · Fixed by #5027
Closed

finish_reason of non-openai models not handled in OpenAIChatCompletionClient #5020

ravishqureshi opened this issue Jan 13, 2025 · 2 comments · Fixed by #5027

Comments

@ravishqureshi
Copy link

What happened?

I plan to use openrouter with autogen given it provides openai api compatible models. I am able to run openai using openrouter. however, if i try to run claude or gemini, i run into this:


ValidationError: 1 validation error for CreateResult
finish_reason
Input should be 'stop', 'length', 'function_calls' or 'content_filter' [type=literal_error, input_value='end_turn', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/literal_error


What did you expect to happen?

I expect a way to easily specify stop reasons and map them to openai standard ones so that we can use any model whose request schema is compatible to openai but response schema may not be.

How can we reproduce it (as minimally and precisely as possible)?

config_list=[
{
"model": "openai/gpt-4o",
"base_url": "https://openrouter.ai/api/v1",
"api_key": openrouter_key,
"api_type": "openai",
"model_info": {
"vision": True,
"function_calling": True,
"json_output": False
}
},
{
"model": "anthropic/claude-3.5-sonnet",
"base_url": "https://openrouter.ai/api/v1",
"api_key": openrouter_key,
"api_type": "anthropic",
"model_info": {
"vision": True,
"function_calling": True,
"json_output": False
}
}
]

config = config_list[1] # Use the openai model config

Extract relevant parameters

model = config["model"]
api_key = config["api_key"]
base_url = config["base_url"]
model_info=config.get("model_info", {})
api_type = config["api_type"]

Initialize the OpenRouterClient

model_client = OpenAIChatCompletionClient(
model=model,
api_key=api_key,
base_url=base_url,
model_info=model_info,
api_type=api_type
)

from autogen_agentchat.agents import AssistantAgent

Initialize the AssistantAgent

agent = AssistantAgent(
name="assistant",
model_client=model_client,
system_message="You are a helpful assistant skilled in multimodal tasks."
)

from autogen_core.models import UserMessage

Run the task with the pre-constructed messages

response = await run_task(agent, agentchat_messages)
print(response)


Result:
ValidationError: 1 validation error for CreateResult
finish_reason
Input should be 'stop', 'length', 'function_calls' or 'content_filter' [type=literal_error, input_value='end_turn', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/literal_error


Works well when i change "config = config_list[1]" to "config = config_list[0]" ie. use openai

AutoGen version

0.4

Which package was this bug in

AgentChat

Model used

claude

Python version

3.12

Operating system

No response

Any additional info you think would be helpful for fixing this bug

I believe there should be a way to map response keys from non-openai models in "CreateResult" so that they can be used

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 13, 2025

@ravishqureshi thanks for the Issue. I believe the stop_reason should allow arbitrary text rather than a Literal for non-OpenAI clients. It's not really meaningful to keep the stop reason a fixed set.

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 13, 2025

Related #4875

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants