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

Python: Bug: Assistant Agent enable json error #10375

Closed
onderyildirim opened this issue Feb 3, 2025 · 1 comment · Fixed by #10376
Closed

Python: Bug: Assistant Agent enable json error #10375

onderyildirim opened this issue Feb 3, 2025 · 1 comment · Fixed by #10376
Labels
agents bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@onderyildirim
Copy link

Describe the bug
Wrong option set when enable_json_response=True

To Reproduce
Steps to reproduce the behavior:
a = await AzureAssistantAgent.create(
kernel=Kernel(),
service_id="agent",
name="a1",
instructions="",
enable_json_response=True,
)
tid = await a.create_thread()
await a.add_chat_message(thread_id=tid,message=ChatMessageContent(role=AuthorRole.USER, content="Return numbers 1 to 10 in json format"))
async for r in a.invoke(thread_id=tid):
ar = r.content
print(ar)

invoke method fails with
File "...\main.py", line 357, in main
async for response in production_optimizer_agent.invoke_stream(thread_id=optimizer_thread_id):
File "....venv\Lib\site-packages\semantic_kernel\agents\open_ai\open_ai_assistant_base.py", line 827, in invoke_stream
async for content in self._invoke_internal_stream(
File "....venv\Lib\site-packages\semantic_kernel\agents\open_ai\open_ai_assistant_base.py", line 905, in _invoke_internal_stream
async with stream as response_stream:
^^^^^^
File "....venv\Lib\site-packages\openai\lib\streaming_assistants.py", line 879, in aenter
self.__stream = await self.__api_request
^^^^^^^^^^^^^^^^^^^^^^^^
File "....venv\Lib\site-packages\openai_base_client.py", line 1839, in post
return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "....venv\Lib\site-packages\openai_base_client.py", line 1533, in request
return await self._request(
^^^^^^^^^^^^^^^^^^^^
File "....venv\Lib\site-packages\openai_base_client.py", line 1634, in _request
raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value: 'json'. Supported values are: 'auto'.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': 'invalid_value'}}

Expected behavior
A response like below
{
"numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

Platform

  • Language: Python

Changing ....venv\Lib\site-packages\semantic_kernel\agents\open_ai\open_ai_assistant_base.py line 1052 (method _generate_options)

from:
"response_format": "json" if merged_options.get("enable_json_response") else None,
to:
"response_format": {"type": "json_object"} if merged_options.get("enable_json_response") else None,

solves the problem.

@onderyildirim onderyildirim added the bug Something isn't working label Feb 3, 2025
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Feb 3, 2025
@github-actions github-actions bot changed the title Bug: Python: Bug: Feb 3, 2025
@shethaadit
Copy link
Contributor

Hi @markwallace-microsoft / @moonbox3, I will raise a quick PR for the fix. :)

@moonbox3 moonbox3 changed the title Python: Bug: Python: Bug: Assistant Agent enable json error Feb 3, 2025
@moonbox3 moonbox3 added agents and removed triage labels Feb 4, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 5, 2025
### Description
Fixed Python bug when enable_json_response=True

- Closes #10375

### Contribution Checklist

- [Y] The code builds clean without any errors or warnings
- [Y] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [Y] All unit tests pass, and I have added new tests where possible
- [Y] I didn't break anyone 😄

Fixes #10375

---------

Co-authored-by: Adit Sheth <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents bug Something isn't working python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants