Malformed Jsons in GPT-4.1-mini

Am I the only one experiencing this? Lately, in the Responses API (GPT-4.1-mini model), I saw that it’s generating invalid JSON. Does this happen to you?

example:

{"section":"JOBS","function-name":"createIncidents","entity":"Jonh","description":"Broken motobike ,"assignee-id':'1111111'','priority":"NORMAL"}

It also does things like this:

{"section":"JOBS","function-name":"createIncidents","entity":"Alice","priority":"HIGH","description\":\"instalation\"} } Assistant can't process because there's an error in the JSON structure. I'll fix it. json fixed: {" :": " , " , " : " "}

Thx :smile:

Please place JSON for diagnosis in backticks for a code block (press the </> button on the forum’s editor).

Otherwise, what is seen here is a bunch of directional quotes that certainly would be invalid JSON, but that’s from the forum’s formatting.

I think you’re showing a single quote around the word priority.

That should not be possible if using a strict structured output.

It would be possible if the JSON is not constrained and you have not added a top_p parameter below 1.0 (such as 0.5), because then every generation is a gamble that you don’t get a bad token selected randomly.

Hello! Thanks for answering my questions :grinning:. The problem is that this is with function calling. It’s a JSON schema with required fields and non-required fields. I don’t know if that’s the problem.

I am new at this, but I have paied my account, but I get “I/O error 105” from this Delphi 12 code:

JsonMsg.AddPair(‘role’, ‘user’);
JsonMsg.AddPair(‘content’, ‘Hello, who are you?’);
MsgsArray.AddElement(JsonMsg);

JsonReq.AddPair(‘model’, ‘gpt-41-mini’);
JsonReq.AddPair(‘messages’, MsgsArray);

JsonStr := JsonReq.ToJSON;
RequestBody := TStringStream.Create(JsonStr, TEncoding.UTF8);

HttpClient := TNetHTTPClient.Create(nil);
try
HttpClient.CustomHeaders[‘Authorization’] := 'Bearer ’ + APIKey;
HttpClient.ContentType := ‘application/json’;

Response := HttpClient.Post('https://api.openai.com/v1/chat/completions', RequestBody);

if Response.StatusCode = 200 then
  Writeln('Response: ' + Response.ContentAsString())
else
  Writeln('Error: ' + Response.StatusCode.ToString + ' - ' + Response.StatusText);

finally
RequestBody.Free;
JsonReq.Free;
HttpClient.Free;
end;

1 Like

Hi! Could it be that the model is not written correctly?

JsonReq.AddPair(‘model’, ‘gpt-41-mini’);

correct model :
JsonReq.AddPair(‘model’, ‘gpt-4.1-mini’);

1 Like

Wow, haven’t used Delphi since my PhD! Kudos! :slight_smile:

1 Like

Try to set JSON strict mode
https://platform.openai.com/docs/guides/function-calling?api-mode=responses

tools = [{
“type”: “function”,
“name”: “get_weather”,
“description”: “Get current temperature for provided coordinates in celsius.”,
“parameters”: {
“type”: “object”,
“properties”: {
“latitude”: {“type”: “number”},
“longitude”: {“type”: “number”}
},
“required”: [“latitude”, “longitude”],
“additionalProperties”: False
},
“strict”: True
}]

EDIT:

I had mistakenly thought the OP was giving an input → output example, and I thought “oh the input was wrong, that’s why the output was wrong”. But actually, they didn’t share an input example, both were output examples.

So really the question for the OP @celia.stelorder is, what was your input?

Hard to look deeper into the processing to figure out what might be possible to correct at the input level if we don’t have any information about the input itself.

Since the openai models are relatively black-box, we don’t know why it produced bad output, but can potentially help with reviewing input and considering if there’s any tweaks that could be provided to make better use of these models which sometimes produce bad output even under strict parameters…

2 Likes

Your failure is you are blaming the bad output on an unseen function specification, blaming the issue on the user. What is shown is the symptom. Not “tell me what is wrong with this symptom”.

It is impossible to pass a function specification that is a malformed JSON schema. You would get an API error.

The AI is the one producing a bad argument object.

Whoever marked this as a solution is daft.


The actual solution is to use a model that doesn’t stink, at near-deterministic sampling parameters, and find a way to construct a strict JSON schema with anyOf that can satisfy the need for optional keys to be produced.

3 Likes

Oh, your right. I went back and read the original post. I for some reason looked at it as input → output which it wasn’t, she was sharing two examples of malformed JSON output, not an input / output pair.

I’ll edit my response, and maybe someone can message Paul and get him to “unmark” my solution.

My mistake, thanks for pointing that out.

1 Like

Don’t think it was me that marked it? Looks like someone unchecked it.

Glad it got resolved.