Tech

anyOf JSON Example for OpenAI API Structured Outputs

Photo of Zac VineyardZac Vineyard
January 30th, 2025

Recently, I was researching OpenAI's Structured Outputs, which ensures that responses adhere to a JSON schema. This was an incredibly powerful idea because some CMS platforms leverage structured data objects like YAML or JSON to store page objects (or other types of content). Below is a JSON Schema for an email object I created. It allowed me to drop JSON (converted to YAML) into a CMS that generates email templates, after having ChatGPT choose the email components and write the content.

In my journey to understand this, I had a hard time figuring out how to leverage the "anyOf" keyword/item in the JSON schema, but I have a working example below that may help you on your quest to using OpenAI's Structured Outputs.

{
    "name": "email_json",
    "strict": true,
    "schema": {
        "type": "object",
        "properties": {
            "id": {
                "type": "string",
                "description": "The UUID of the email."
            },
            "blueprint": {
                "type": "string",
                "description": "The blueprint used by the email.",
                "items": {
                    "type": "string",
                    "enum": [
                        "rss_to_mjml"
                    ]
                }
            },
            "header": {
                "type": "string",
                "description": "The header used by the email.",
                "items": {
                    "type": "string",
                    "enum": [
                        "claas",
                        "miami"
                    ]
                }
            },
            "footer": {
                "type": "string",
                "description": "The footer used by the email.",
                "items": {
                    "type": "string",
                    "enum": [
                        "claas",
                        "miami"
                    ]
                }
            },
            "author": {
                "type": "number",
                "description": "The ID of the email's author.",
                "items": {
                    "enum": [
                        1
                    ]
                }
            },
            "content": {
            	"type": "array",
            	"items": {
            		"anyOf": [
            			{
			                "type": "object",
			                "properties": {
        	                	"paragraph_set": {
        							"type": "string",
        							"value": ""
        						},
			                	"type": {
									"type": "string",
									"value": "paragraph"
								},
					            "content": {
					                "type": "array",
					                "items": {
					                	"type": "object",
						                "properties": {
						                	"type": {
												"type": "string",
												"value": "text"
											},
						                	"text": {
												"type": "string",
												"description": "Block text for the email. This cannot contain line breaks."
											}
										},
								        "required": [
								            "type",
								            "text"
								        ],
										"additionalProperties": false
									}
					            }
							},
					        "required": [
					        	"paragraph_set",
					            "type",
					            "content"
					        ],
							"additionalProperties": false
						},
            			{
			                "type": "object",
			                "properties": {
        	                	"divider_set": {
        							"type": "string",
        							"value": ""
        						},
			                	"type": {
									"type": "string",
									"value": "set"
								},
								"attrs": {
									"type": "object",
									"properties": {
					                	"id": {
											"type": "string",
											"value": "set"
										},
										"values": {
											"type": "object",
											"properties": {
							                	"type": {
													"type": "string",
													"value": "divider"
												},
							                	"style": {
													"type": "string",
													"value": "dashed"
												}
											},
									        "required": [
									            "type",
									            "style"
									        ],
											"additionalProperties": false
										}
									},
							        "required": [
							            "id",
							            "values"
							        ],
									"additionalProperties": false
								}
							},
					        "required": [
					        	"divider_set",
					            "type",
					            "attrs"
					        ],
							"additionalProperties": false
						},
            			{
			                "type": "object",
			                "properties": {
        	                	"news_story_set": {
        							"type": "string",
        							"value": ""
        						},
			                	"type": {
									"type": "string",
									"value": "set"
								},
								"attrs": {
									"type": "object",
									"properties": {
					                	"id": {
											"type": "string",
											"description": "A short and random string of letters and numbers."
										},
										"values": {
											"type": "object",
											"properties": {
							                	"type": {
													"type": "string",
													"value": "news_story"
												},
							                	"story_url": {
													"type": "string",
													"items": {
													    "type": "string",
													    "enum": [
													        "https://miamioh.edu/news/2024/12/what-did-visitors-to-miamis-news-site-view-most-in-2024.html"
													    ]
													}
												}
											},
									        "required": [
									            "type",
									            "story_url"
									        ],
											"additionalProperties": false
										}
									},
							        "required": [
							            "id",
							            "values"
							        ],
									"additionalProperties": false
								}
							},
					        "required": [
					        	"news_story_set",
					            "type",
					            "attrs"
					        ],
							"additionalProperties": false
						},
						{
			                "type": "object",
			                "properties": {
        	                	"h1_set": {
        							"type": "string",
        							"value": ""
        						},
			                	"type": {
									"type": "string",
									"value": "heading"
								},
			                	"attrs": {
									"type": "number",
									"value": 1
								},
								"content": {
					                "type": "array",
					                "items": {
					                	"type": "object",
						                "properties": {
						                	"type": {
												"type": "string",
												"value": "text"
											},
						                	"text": {
												"type": "string"
											}
										},
								        "required": [
								            "type",
								            "text"
								        ],
										"additionalProperties": false
									}
					            }
							},
					        "required": [
					        	"h1_set",
					            "type",
					            "attrs",
					            "content"
					        ],
							"additionalProperties": false
						}
					]
            	}
            }
        },
        "required": [
            "id",
            "blueprint",
            "author",
            "content",
            "header",
            "footer"
        ],
        "additionalProperties": false
    }
}

Copyright © Zachary Vineyard