Skip to content

.Net: Bug: JsonReaderException when OpenAI ChatCompletion returns empty or invalid response in Semantic Kernel #13422

@akshay-msft-09

Description

@akshay-msft-09

Describe the bug
When calling Kernel.InvokeAsync() with a prompt that uses an OpenAI Chat Completion service, the execution fails with:

System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true.  
   at System.Text.Json.JsonDocument.Parse(...)  
   at OpenAI.Chat.ChatCompletion.op_Explicit(ClientResult result)  
   at OpenAI.Chat.ChatClient.CompleteChatAsync(...)  
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](...)  

It appears that the Semantic Kernel connector is attempting to parse an empty or non‑JSON response from the model.

To Reproduce

  • Create a Kernel instance with the OpenAI Chat Completion connector (IChatCompletionService).
  • Pass in a prompt to KernelFunctionFromPrompt.InvokeAsync() that results in an empty or malformed API response.
  • Observe that the SDK throws a JsonReaderException instead of handling the empty/malformed response gracefully.
  • Expected behavior
  • Semantic Kernel should handle empty or invalid JSON responses from the model gracefully — either by returning a clear error message or retrying — instead of throwing an unhandled exception.

Platform

Language: C#
Semantic Kernel Source: NuGet package (please specify exact version, e.g., 1.12.0)

    <PackageVersion Include="Microsoft.SemanticKernel" Version="1.65.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.33.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Agents.Abstractions" Version="1.45.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Agents.AzureAI" Version="1.66.0-preview" />
    <PackageVersion Include="Microsoft.SemanticKernel.Agents.Core" Version="1.45.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="0.17.230704.3-preview" />
    <PackageVersion Include="Microsoft.SemanticKernel.Connectors.AzureAISearch" Version="1.33.0-preview" />
    <PackageVersion Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" Version="1.65.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Connectors.Memory.AzureAISearch" Version="1.0.0-rc3" />
    <PackageVersion Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.33.0" />
    <PackageVersion Include="Microsoft.SemanticKernel.Planners.Handlebars" Version="1.33.0-preview" />
    <PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.33.0-alpha" />

AI Model: OpenAI GPT‑4o‑mini (or whichever model you used) : GPT o3 mini (Behind Azure API Management)
Additional context
The issue appears to be triggered when the OpenAI service returns an unexpected payload (possibly empty). This could happen if:

  • The request was malformed.
  • The model returned no content due to filtering.
  • There was an upstream network or API gateway issue.
  • Suggest adding null/empty checks before parsing JSON in OpenAI.Chat.ChatCompletion.op_Explicit or in ClientCore.RunRequestAsync.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codebugSomething isn't workingtriage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions