2114 2114
2115```python2115```python
2116from openai import OpenAI2116from openai import OpenAI
2117from jsonschema import Draft202012Validator, ValidationError
2117import json2118import json
2118 2119
2119client = OpenAI()2120client = OpenAI()
2120 2121
2121META_SCHEMA = {2122SCHEMA_DEFINITION = {
2122 "name": "metaschema",
2123 "schema": {
2124 "type": "object",2123 "type": "object",
2125 "properties": {2124 "properties": {
2126 "name": {"type": "string", "description": "The name of the schema"},2125 "description": {"type": "string"},
2127 "type": {2126 "type": {
2128 "type": "string",2127 "type": "string",
2129 "enum": ["object", "array", "string", "number", "boolean", "null"],2128 "enum": ["object", "array", "string", "number", "boolean", "null"],
2140 },2139 },
2141 "required": {"type": "array", "items": {"type": "string"}},2140 "required": {"type": "array", "items": {"type": "string"}},
2142 "additionalProperties": {"type": "boolean"},2141 "additionalProperties": {"type": "boolean"},
2143 },2142 "enum": {"type": "array", "minItems": 1, "uniqueItems": True},
2144 "required": ["type"],2143 "$ref": {"type": "string"},
2145 "additionalProperties": False,
2146 "if": {"properties": {"type": {"const": "object"}}},
2147 "then": {"required": ["properties"]},
2148 "$defs": {2144 "$defs": {
2149 "schema_definition": {
2150 "type": "object",
2151 "properties": {
2152 "type": {
2153 "type": "string",
2154 "enum": [
2155 "object",
2156 "array",
2157 "string",
2158 "number",
2159 "boolean",
2160 "null",
2161 ],
2162 },
2163 "properties": {
2164 "type": "object",2145 "type": "object",
2165 "additionalProperties": {"$ref": "#/$defs/schema_definition"},2146 "additionalProperties": {"$ref": "#/$defs/schema_definition"},
2166 },2147 },
2167 "items": {2148 "anyOf": {
2168 "anyOf": [
2169 {"$ref": "#/$defs/schema_definition"},
2170 {
2171 "type": "array",2149 "type": "array",
2150 "minItems": 1,
2172 "items": {"$ref": "#/$defs/schema_definition"},2151 "items": {"$ref": "#/$defs/schema_definition"},
2173 },2152 },
2174 ]
2175 },
2176 "required": {"type": "array", "items": {"type": "string"}},
2177 "additionalProperties": {"type": "boolean"},
2178 },2153 },
2179 "required": ["type"],
2180 "additionalProperties": False,2154 "additionalProperties": False,
2181 "if": {"properties": {"type": {"const": "object"}}},2155 "if": {"required": ["type"], "properties": {"type": {"const": "object"}}},
2182 "then": {"required": ["properties"]},2156 "then": {"required": ["properties"]},
2157}
2158
2159META_SCHEMA = {
2160 "name": "metaschema",
2161 "schema": {
2162 **SCHEMA_DEFINITION,
2163 "required": ["type"],
2164 "properties": {
2165 "name": {"type": "string", "description": "The name of the schema"},
2166 **SCHEMA_DEFINITION["properties"],
2167 },
2168 "$defs": {
2169 "schema_definition": {
2170 **SCHEMA_DEFINITION,
2171 "anyOf": [
2172 {"required": ["type"]},
2173 {"required": ["$ref"]},
2174 {"required": ["anyOf"]},
2175 ],
2183 }2176 }
2184 },2177 },
2185 },2178 },
2353 2346
2354 2347
2355def generate_schema(description: str):2348def generate_schema(description: str):
2349 messages = [
2350 {"role": "system", "content": META_PROMPT},
2351 {"role": "user", "content": "Description:\n" + description},
2352 ]
2353 validator = Draft202012Validator(META_SCHEMA["schema"])
2354 # Generation is non-strict. Validate locally and allow one correction.
2355 for attempt in range(2):
2356 completion = client.chat.completions.create(2356 completion = client.chat.completions.create(
2357 model="gpt-5.6-terra",2357 model="gpt-5.6-terra",
2358 response_format={"type": "json_schema", "json_schema": META_SCHEMA},2358 response_format={"type": "json_schema", "json_schema": META_SCHEMA},
2359 messages=[2359 messages=messages,
2360 {2360 )
2361 "role": "system",2361 choice = completion.choices[0]
2362 "content": META_PROMPT,2362 if choice.finish_reason != "stop" or choice.message.refusal:
2363 },2363 raise ValueError("Schema generation did not complete successfully")
2364 content = choice.message.content
2365 schema = json.loads(content)
2366 try:
2367 validator.validate(schema)
2368 except ValidationError as error:
2369 if attempt == 1:
2370 raise
2371 messages.extend(
2372 [
2373 {"role": "assistant", "content": content},
2364 {2374 {
2365 "role": "user",2375 "role": "user",
2366 "content": "Description:\n" + description,2376 "content": (
2377 "Correct the schema to satisfy the original meta-schema. "
2378 f"Validation error at {error.json_path}: {error.message}. "
2379 "Return only the corrected JSON."
2380 ),
2367 },2381 },
2368 ],2382 ]
2369 )2383 )
2370 2384 else:
2371 return json.loads(completion.choices[0].message.content)2385 return schema
2372```2386```
2373 2387
2374```go2388```go
2561 "type": "string",2575 "type": "string",
2562 "description": "The name of the schema"2576 "description": "The name of the schema"
2563 },2577 },
2578 "description": {
2579 "type": "string"
2580 },
2564 "type": {2581 "type": {
2565 "type": "string",2582 "type": "string",
2566 "enum": [2583 "enum": [
2599 },2616 },
2600 "additionalProperties": {2617 "additionalProperties": {
2601 "type": "boolean"2618 "type": "boolean"
2619 },
2620 "enum": {
2621 "type": "array",
2622 "minItems": 1,
2623 "uniqueItems": true
2624 },
2625 "$ref": {
2626 "type": "string"
2627 },
2628 "$defs": {
2629 "type": "object",
2630 "additionalProperties": {
2631 "$ref": "#/$defs/schema_definition"
2632 }
2633 },
2634 "anyOf": {
2635 "type": "array",
2636 "minItems": 1,
2637 "items": {
2638 "$ref": "#/$defs/schema_definition"
2639 }
2602 }2640 }
2603 },2641 },
2642 "additionalProperties": false,
2643 "if": {
2604 "required": [2644 "required": [
2605 "type"2645 "type"
2606 ],2646 ],
2607 "additionalProperties": false,
2608 "if": {
2609 "properties": {2647 "properties": {
2610 "type": {2648 "type": {
2611 "const": "object"2649 "const": "object"
2617 "properties"2655 "properties"
2618 ]2656 ]
2619 },2657 },
2658 "required": [
2659 "type"
2660 ],
2620 "$defs": {2661 "$defs": {
2621 "schema_definition": {2662 "schema_definition": {
2622 "type": "object",2663 "type": "object",
2662 },2703 },
2663 "additionalProperties": {2704 "additionalProperties": {
2664 "type": "boolean"2705 "type": "boolean"
2706 },
2707 "enum": {
2708 "type": "array",
2709 "minItems": 1,
2710 "uniqueItems": true
2711 },
2712 "$ref": {
2713 "type": "string"
2714 },
2715 "$defs": {
2716 "type": "object",
2717 "additionalProperties": {
2718 "$ref": "#/$defs/schema_definition"
2719 }
2720 },
2721 "anyOf": {
2722 "type": "array",
2723 "minItems": 1,
2724 "items": {
2725 "$ref": "#/$defs/schema_definition"
2726 }
2665 }2727 }
2666 },2728 },
2729 "additionalProperties": false,
2730 "if": {
2667 "required": [2731 "required": [
2668 "type"2732 "type"
2669 ],2733 ],
2670 "additionalProperties": false,
2671 "if": {
2672 "properties": {2734 "properties": {
2673 "type": {2735 "type": {
2674 "const": "object"2736 "const": "object"
2679 "required": [2741 "required": [
2680 "properties"2742 "properties"
2681 ]2743 ]
2744 },
2745 "anyOf": [
2746 {
2747 "required": [
2748 "type"
2749 ]
2750 },
2751 {
2752 "required": [
2753 "$ref"
2754 ]
2755 },
2756 {
2757 "required": [
2758 "anyOf"
2759 ]
2682 }2760 }
2761 ]
2683 }2762 }
2684 }2763 }
2685}`), &schema); err != nil {2764}`), &schema); err != nil {
3350require "openai"3429require "openai"
3351require "json"3430require "json"
3352 3431
3353META_SCHEMA = {3432SCHEMA_DEFINITION = {
3354 "name" => "metaschema",
3355 "schema" => {
3356 "type" => "object",3433 "type" => "object",
3357 "properties" => {3434 "properties" => {
3358 "description" => { "type" => "string" },3435 "description" => {
3359 "name" => {3436 "type" => "string"
3360 "type" => "string",
3361 "description" => "The name of the schema"
3362 },3437 },
3363 "type" => {3438 "type" => {
3364 "type" => "string",3439 "type" => "string",
3365 "enum" => ["object", "array", "string", "number", "boolean", "null"]3440 "enum" => [
3441 "object",
3442 "array",
3443 "string",
3444 "number",
3445 "boolean",
3446 "null"
3447 ]
3366 },3448 },
3367 "properties" => {3449 "properties" => {
3368 "type" => "object",3450 "type" => "object",
3374 "anyOf" => [3456 "anyOf" => [
3375 {3457 {
3376 "$ref" => "#/$defs/schema_definition"3458 "$ref" => "#/$defs/schema_definition"
3377 }, {3459 },
3460 {
3378 "type" => "array",3461 "type" => "array",
3379 "items" => {3462 "items" => {
3380 "$ref" => "#/$defs/schema_definition"3463 "$ref" => "#/$defs/schema_definition"
3390 },3473 },
3391 "additionalProperties" => {3474 "additionalProperties" => {
3392 "type" => "boolean"3475 "type" => "boolean"
3393 }
3394 },3476 },
3395 "required" => ["type"],3477 "enum" => {
3396 "additionalProperties" => false,3478 "type" => "array",
3397 "if" => {3479 "minItems" => 1,
3398 "properties" => {3480 "uniqueItems" => true
3399 "type" => {
3400 "const" => "object"
3401 }
3402 }
3403 },3481 },
3404 "then" => {3482 "$ref" => {
3405 "required" => ["properties"]3483 "type" => "string"
3406 },3484 },
3407 "$defs" => {3485 "$defs" => {
3408 "schema_definition" => {
3409 "type" => "object",
3410 "properties" => {
3411 "description" => { "type" => "string" },
3412 "type" => {
3413 "type" => "string",
3414 "enum" => ["object", "array", "string", "number", "boolean", "null"]
3415 },
3416 "properties" => {
3417 "type" => "object",3486 "type" => "object",
3418 "additionalProperties" => {3487 "additionalProperties" => {
3419 "$ref" => "#/$defs/schema_definition"3488 "$ref" => "#/$defs/schema_definition"
3420 }3489 }
3421 },3490 },
3422 "items" => {3491 "anyOf" => {
3423 "anyOf" => [
3424 {
3425 "$ref" => "#/$defs/schema_definition"
3426 }, {
3427 "type" => "array",3492 "type" => "array",
3493 "minItems" => 1,
3428 "items" => {3494 "items" => {
3429 "$ref" => "#/$defs/schema_definition"3495 "$ref" => "#/$defs/schema_definition"
3430 }3496 }
3431 }3497 }
3432 ]
3433 },
3434 "required" => {
3435 "type" => "array",
3436 "items" => {
3437 "type" => "string"
3438 }
3439 },
3440 "additionalProperties" => {
3441 "type" => "boolean"
3442 }
3443 },3498 },
3444 "required" => ["type"],
3445 "additionalProperties" => false,3499 "additionalProperties" => false,
3446 "if" => {3500 "if" => {
3501 "required" => [
3502 "type"
3503 ],
3447 "properties" => {3504 "properties" => {
3448 "type" => {3505 "type" => {
3449 "const" => "object"3506 "const" => "object"
3451 }3508 }
3452 },3509 },
3453 "then" => {3510 "then" => {
3454 "required" => ["properties"]3511 "required" => [
3455 }3512 "properties"
3513 ]
3456 }3514 }
3515}
3516
3517META_SCHEMA = {
3518 "name" => "metaschema",
3519 "schema" => SCHEMA_DEFINITION.merge(
3520 "required" => ["type"],
3521 "properties" => SCHEMA_DEFINITION.fetch("properties").merge(
3522 "name" => {
3523 "type" => "string",
3524 "description" => "The name of the schema"
3457 }3525 }
3526 ),
3527 "$defs" => {
3528 "schema_definition" => SCHEMA_DEFINITION.merge(
3529 "anyOf" => [{ "required" => ["type"] }, { "required" => ["$ref"] }, { "required" => ["anyOf"] }]
3530 )
3458 }3531 }
3532 )
3459}3533}
3460 3534
3461META_PROMPT = <<~PROMPT.strip3535META_PROMPT = <<~PROMPT.strip
3922 "schema_definition": {3996 "schema_definition": {
3923 "type": "object",3997 "type": "object",
3924 "properties": {3998 "properties": {
3999 "description": {"type": "string"},
4000 "enum": {"type": "array", "minItems": 1, "uniqueItems": True},
3925 "type": {4001 "type": {
3926 "type": "string",4002 "type": "string",
3927 "enum": [4003 "enum": [
4316 },4392 },
4317 "additionalProperties": {4393 "additionalProperties": {
4318 "type": "boolean"4394 "type": "boolean"
4395 },
4396 "enum": {
4397 "type": "array",
4398 "minItems": 1,
4399 "uniqueItems": true
4319 }4400 }
4320 },4401 },
4321 "required": [4402 "required": [
4827 "type" => "object",4908 "type" => "object",
4828 "properties" => {4909 "properties" => {
4829 "description" => { "type" => "string" },4910 "description" => { "type" => "string" },
4911 "enum" => {
4912 "type" => "array",
4913 "minItems" => 1,
4914 "uniqueItems" => true
4915 },
4830 "type" => {4916 "type" => {
4831 "type" => "string",4917 "type" => "string",
4832 "enum" => ["object", "array", "string", "number", "boolean", "null"]4918 "enum" => ["object", "array", "string", "number", "boolean", "null"]