Duplicated structured output content

Hi,
I often run into the following error in python when parsing a structured output with json:
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 292)

I am using the following code:

completion = self.openai_client.chat.completions.create(
            model="gpt-4o-2024-08-06",
            messages=messages,
            tools=self.tools,
            parallel_tool_calls=False,
            response_format={
                "type": "json_schema",
                "json_schema": {
                    "name": "reasoning_schema",
                    "strict": True,
                    "schema": {
                        "type": "object",
                        "properties": {
                            "reasoning_steps": {
                                "type": "array",
                                "items": {"type": "string"},
                                "description": "The reasoning steps leading to the final conclusion.",
                            },
                            "answer": {
                                "type": "string",
                                "description": "The final answer, taking into account the reasoning steps.",
                            },
                        },
                        "required": ["reasoning_steps", "answer"],
                        "additionalProperties": False,
                    },
                },
            },
            seed=0,
        )

Upon investigation I found out that the API returns two consecutive outputs in the same string like the following (printing completion.choices[0].message.content):
{“reasoning_steps”:[“step1”,“step2”,“…”],“answer”:“answer”}
{“reasoning_steps”:[“differentstep1”,“differentstep2”,“…”],“answer”:“differentanswer”}

Any ideas on what might be the cause or how to handle this?

2 Likes

We had a same problem, wondering have you managed to solve this?