Python: Bug: Structured Output fails when ChatCompletionAgent is configured with AzureAIInferenceChatCompletion
Describe the bug
When following the instructions [1] everything operational when run AS-IS. However if I switch to use AzureAIInferenceChatCompletion, the code fails with JSONDecodeError. The model does provide a correct response, but the invocation does not return Structured Output.
Attaching the code sample to reproduce
[1] https://github.com/microsoft/semantic-kernel/blob/main/python/samples/getting_started_with_agents/chat_completion/step10_chat_completion_agent_structured_outputs.py
To Reproduce Steps to reproduce the behavior:
- Open the referenced file [1]
- Update the file to use "AzureAIInferenceChatCompletion" (see below the code sample)
- Run the file an see the error
Expected behavior To execute and produce the output as per example in the file - which is a copy of [1]
Platform
- Language: Python
- Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository]
- AI model: Azure OpenAI: GPT-4.1
- IDE: VS Code
- OS: Mac
AzureAIInferenceChatCompletion
# ----------------------------------------------------------------------
# Commented out: when using AzureChatCompletion, code works.
# ----------------------------------------------------------------------
# service=AzureChatCompletion(
# endpoint=endpoint,
# api_version=api_version,
# api_key=api_key,
# deployment_name=MODEL_NAME,
# )
# ----------------------------------------------------------------------
# When using AzureAIInferenceChatCompletion, code fails
# ----------------------------------------------------------------------
from semantic_kernel.connectors.ai.azure_ai_inference import AzureAIInferenceChatCompletion
from azure.ai.inference.aio import ChatCompletionsClient
from azure.identity.aio import DefaultAzureCredential
service = AzureAIInferenceChatCompletion(
ai_model_id="executor",
service_id="executor",
client=ChatCompletionsClient(
endpoint=f"{str(api_base).strip('/')}/openai/deployments/{MODEL_NAME}",
api_version=api_version,
credential=DefaultAzureCredential(),
credential_scopes=["https://cognitiveservices.azure.com/.default"]
))
```
The AzureAIInference SDK only supports "json_object" mode and not structured outputs. https://github.com/microsoft/semantic-kernel/issues/11025#issuecomment-2789106777
Closing in favor of: https://github.com/microsoft/semantic-kernel/issues/11952