# How to print a Structured Output like a JSON?

Question: When using Structured Output, how do I edit repr or str, or how do I print the text nicely with identations?

When I used `pprint`, it didn’t print the entire structure nicely like a JSON file would be printed.

Example: How do I turn this:

``````"MathReasoning(steps=Step(explanation="Start with the equation 8x + 7 = -23.",output="8x + 7 = -23"), Step(explanation="Subtract 7 from both sides to isolate the term with the variable.", output="8x = -23 - 7"), Step(explanation="Simplify the right side of the equation.", output="8x = -30", Step(explanation="Divide both sides by 8 to solve for x.", output="x = -30 / 8", Step(explanation="Simplify the fraction.", output="x = -15 / 4"), final_answer="x = -15 / 4")
``````

into this?

``````{"steps": [
{
"output": "8x + 7 = -23"
},
{
"explanation": "Subtract 7 from both sides to isolate the term with the variable.",
"output": "8x = -23 - 7"
},
{
"explanation": "Simplify the right side of the equation.",
"output": "8x = -30"
},
{
"explanation": "Divide both sides by 8 to solve for x.",
"output": "x = -30 / 8"
},
{
"explanation": "Simplify the fraction.",
"output": "x = -15 / 4"
}
],
"final_answer": "x = -15 / 4"
}
``````

Hey guys, I couldn’t find a high-level function that parses the structured output into JSON (or even Dictionary).

I preferred not to use the format_response=json_schema, since it was said in the documentation:

JSON mode is a more basic version of the Structured Outputs feature. While JSON mode ensures that model output is valid JSON, Structured Outputs reliably matches the model’s output to the schema you specify. We recommend you use Structured Outputs if it is supported for your use case.

Instead, I’ve created a nested dict comprehension function that iterates through the sub-attributes of each.

It would looks something like this:

``````def convert_response_to_dict(response):
result = { 'steps' : [{'explanation' : step.explanation, 'output' : step.output} for step in response.steps],
}
return {'MathReasoning' : result}
``````

This ain’t clever, but that’s what I got to work for me right now.

Maybe there’s something more inherent in the API, but I haven’t read it in the documentation.

Feel welcome to contribute a better solution.

Please note that it only converts the response to a dictionary object. Then you have to use `json` to convert to JSON object.

I’ve updated the Custom GPT with the new docs from OpenAI that they added for Structured Output. If anyone wants to talk to the docs, here you go… ChatGPT

1 Like

The issue is that the output from your API call is a Pydantic model instance, not directly in JSON format. You can use Pydantic’s `model_dump(mode='json')` method to convert this output into a JSON-compatible format. To format the JSON output with indentation for readability, you can use Python’s `json.dumps()` method. Here’s how you can do it:

``````import pydantic
import json

# Get the output from your API call
output = completion.choices[0].message.parsed

# Dump the model to a dictionary with JSON-compatible formatting
output_dict = output.model_dump(mode='json')

# Print the dictionary with indentation using json.dumps()
print(json.dumps(output_dict, indent=4))
``````