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

Document structured output for for AssistantAgent in its API doc, and note strict=True is required for FunctionTool #5562

Open
chengyu-liu-cs opened this issue Feb 15, 2025 · 8 comments
Labels
documentation Improvements or additions to documentation proj-agentchat
Milestone

Comments

@chengyu-liu-cs
Copy link

chengyu-liu-cs commented Feb 15, 2025

What feature would you like to be added?

Current AssistantAgent does not support response_format for structured output.
extra_create_args={ "response_format": "Pydantic model", }
It would be perfect if respose_format can be supported by both on_messages and on_messages_stream.

Why is this needed?

It is a good feature supported by many models to return structured output. It would simplify integration with other application in cases where predefined structures are expected from AssistantAgent.

It seems it is supported by OpenAIChatCompletionClient

@ekzhu
Copy link
Collaborator

ekzhu commented Feb 15, 2025

You can pass the client with structured output into the agent, which will then generate structured output: https://microsoft.github.io/autogen/stable/user-guide/agentchat-user-guide/tutorial/agents.html#structured-output

Make sure your FunctionTool is set to strict=True when using this.

@ekzhu ekzhu added this to the 0.4.7-python milestone Feb 15, 2025
@ekzhu ekzhu changed the title response_format parameter support for AssistantAgent Document structured output for for AssistantAgent in its API doc, and note strict=True is required for FunctionTool Feb 15, 2025
@ekzhu ekzhu added documentation Improvements or additions to documentation proj-agentchat labels Feb 15, 2025
@ekzhu ekzhu modified the milestones: 0.4.7-python, python-v0.4.8 Feb 15, 2025
@chengyu-liu-cs
Copy link
Author

@ekzhu , It would be different right if the response_format is configured at client and create levels. If it sets in client level, all the create will generate the structured outputs. While configuring at create level should provide much better flexibility.

For example, if a user says "hello", then the output should be just "hello" instead of enforcing structured outputs.

@chengyu-liu-cs
Copy link
Author

chengyu-liu-cs commented Feb 16, 2025

@ekzhu ,
I was testing the example in the link you provided. Instead of testing the example as it is, I was testing with model_client_stream=True,. There was an error below. Is this something AutoGen will work on ? Is it possible to stream in this scenario, and is there workaround ?
It seems from OpenAI side this is possible

"""
autogen/lib/python3.10/site-packages/openai/resources/chat/completions.py", line 1786, in validate_response_format raise TypeError( TypeError: You tried to pass a BaseModelclass tochat.completions.create(); You must use beta.chat.completions.parse() instead
"""

@ekzhu
Copy link
Collaborator

ekzhu commented Feb 16, 2025

This is indeed a bug in the model client. Can you create a separate issue for this?

Open AI's client uses a different api for structure output. I think the create_stream method has not been updated to use it.

@ekzhu
Copy link
Collaborator

ekzhu commented Feb 16, 2025

@ekzhu , It would be different right if the response_format is configured at client and create levels. If it sets in client level, all the create will generate the structured outputs. While configuring at create level should provide much better flexibility.

For example, if a user says "hello", then the output should be just "hello" instead of enforcing structured outputs.

How does one know when to use structured outputs and when not to? The model cannot decide this. When you pass in a base model in the response format in create method, it will still enforce the structure output. So it becomes the same thing as passing response format in the client which gets passed into the agent.

Perhaps some dynamic approach to detect user intent and then decide whether to use structured output is possible. However, the AssistantAgent is already quite complex and if we add more complex behaviour it will become confusing.

@chengyu-liu-cs
Copy link
Author

Perhaps some dynamic approach to detect user intent and then decide whether to use structured output is possible. However, the AssistantAgent is already quite complex and if we add more complex behaviour it will become confusing.

Exactly. First, detect intents and then dynamically decide if tools and response_format should be attached in model calls.

Any tips or example implementations if this would not be included in AssistantAgent ?

@chengyu-liu-cs
Copy link
Author

This is indeed a bug in the model client. Can you create a separate issue for this?

Open AI's client uses a different api for structure output. I think the create_stream method has not been updated to use it.

Yes. I will create a new issue for this.

@ekzhu
Copy link
Collaborator

ekzhu commented Feb 18, 2025

Any tips or example implementations if this would not be included in AssistantAgent ?

I think this can be a great idea for a custom agent that is meant to be a multi-purpose agent. Or, perhaps a selector group chat with dynamic routing based on user's intent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation proj-agentchat
Projects
None yet
Development

No branches or pull requests

3 participants