Skip to content

[BUG] "gpt-4.1" JsonReaderException for CompleteAsync #49662

@msqishengc

Description

@msqishengc

Library name and version

Azure.AI.Inference 1.0.0-beta.4

Describe the bug

I have code to guard no break when updating model version, while my code is working well with gpt-4o with Azure Inference SDK, just changing the deployment model to version gpt-4.1, it will throw error:

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. LineNumber: 0 | BytePositionInLine: 0.

Stack Trace: 
ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan1 bytes) Utf8JsonReader.Read() JsonDocument.Parse(ReadOnlySpan1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)
JsonDocument.Parse(ReadOnlyMemory1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter) ChatCompletions.FromResponse(Response response) <CompleteAsync>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) TaskAwaiter1.GetResult()
d__3.MoveNext() line 33
--- End of stack trace from previous location where exception was thrown ---
ExceptionDispatchInfo.Throw()
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Here is the code:
`

[TestClass]
public class TestAzureAIInferenceWithSDK
{
Uri endpointUri = new Uri("https://.services.ai.azure.com/models");
string apiKey = "*****";
string deploymentName = "gpt-4.1"; // "gpt-4o";

 [TestMethod]
 public async Task SimpleTextPromptTest()
 {
     ChatCompletionsClient chatCompletionsClient = new ChatCompletionsClient(endpointUri, new AzureKeyCredential(apiKey));

     var requestOptions = new ChatCompletionsOptions()
     {
         Messages =
         {
             new ChatRequestSystemMessage("You are a helpful assistant"),
             new ChatRequestUserMessage("What is the result of 1+1"),
         },
         Model = deploymentName,
         MaxTokens = 800
     };

     var txtResult = await chatCompletionsClient.CompleteAsync(requestOptions);
     var rawResponse = txtResult.GetRawResponse();

     Assert.IsNotNull(rawResponse);
     Assert.AreEqual(200, rawResponse.Status);

     Assert.IsNotNull(txtResult.Value);
     Assert.IsNotNull(txtResult.Value.Content);
 }

}

`

Expected behavior

chatCompletionsClient.CompleteAsync function should work for this simple prompt for gpt-4.1 model
("What is the result of 1+1"),

Actual behavior

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. LineNumber: 0 | BytePositionInLine: 0.

Reproduction Steps

  1. Deploy a gpt-4.1 model (version 2025-04-14) in Azure AI foundry:
Image
  1. Create a C# console or unit test project, write sample code to ask ("What is the result of 1+1"),

`
// Replace endpointUri,apiKey and deploymentName
[TestMethod]
public async Task SimpleTextPromptTest()
{
ChatCompletionsClient chatCompletionsClient = new ChatCompletionsClient(endpointUri, new AzureKeyCredential(apiKey));

 var requestOptions = new ChatCompletionsOptions()
 {
     Messages =
     {
         new ChatRequestSystemMessage("You are a helpful assistant"),
         new ChatRequestUserMessage("What is the result of 1+1"),
     },
     Model = deploymentName,
     MaxTokens = 800
 };

 var txtResult = await chatCompletionsClient.CompleteAsync(requestOptions);
 var rawResponse = txtResult.GetRawResponse();

 Assert.IsNotNull(rawResponse);
 Assert.AreEqual(200, rawResponse.Status);

 Assert.IsNotNull(txtResult.Value);
 Assert.IsNotNull(txtResult.Value.Content);

}

`

Environment

Windows 11
Microsoft Visual Studio Enterprise 2022 (2) (64-bit) Version 17.13.6
.NETFramework 4.8 and .NET 8 also tried

Metadata

Metadata

Assignees

Labels

AI Model InferenceClientThis issue is related to a non-management packageService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions