go/index.md +32 −14
282 282
283// Structured Outputs uses a subset of JSON schema283// Structured Outputs uses a subset of JSON schema
284// These flags are necessary to comply with the subset284// These flags are necessary to comply with the subset
285285func GenerateSchema[T any]() map[string]any {func GenerateSchema[T any]() (map[string]any, error) {
286 reflector := jsonschema.Reflector{286 reflector := jsonschema.Reflector{
287 AllowAdditionalProperties: false,287 AllowAdditionalProperties: false,
288 DoNotReference: true,288 DoNotReference: true,
290 var v T290 var v T
291 schema := reflector.Reflect(v)291 schema := reflector.Reflect(v)
292 292
293293 data, _ := json.Marshal(schema) data, err := json.Marshal(schema)
294294 var result map[string]any if err != nil {
295295 json.Unmarshal(data, &result) return nil, err
296296 return result }
297 // Preserve schema values as raw JSON so integer constraints are not rounded
298 // through float64 before the SDK serializes the request.
299 var rawSchema map[string]json.RawMessage
300 if err := json.Unmarshal(data, &rawSchema); err != nil {
301 return nil, err
302 }
303 result := make(map[string]any, len(rawSchema))
304 for key, value := range rawSchema {
305 result[key] = value
306 }
307 return result, nil
297}308}
298 309
299// Generate the JSON schema at initialization time
300var HistoricalComputerSchema = GenerateSchema[HistoricalComputer]()
301
302func main() {310func main() {
303 client := openai.NewClient()311 client := openai.NewClient()
304 ctx := context.Background()312 ctx := context.Background()
313 schema, err := GenerateSchema[HistoricalComputer]()
314 if err != nil {
315 panic(err)
316 }
305 317
306 response, err := client.Responses.New(ctx, responses.ResponseNewParams{318 response, err := client.Responses.New(ctx, responses.ResponseNewParams{
307 Model: openai.ChatModelGPT5_2,319 Model: openai.ChatModelGPT5_2,
309 OfString: openai.String("What computer ran the first neural network?"),321 OfString: openai.String("What computer ran the first neural network?"),
310 },322 },
311 Text: responses.ResponseTextConfigParam{323 Text: responses.ResponseTextConfigParam{
312324 Format: responses.ResponseFormatTextConfigParamOfJSONSchema( Format: responses.ResponseFormatTextConfigUnionParam{
313325 "historical_computer", OfJSONSchema: &responses.ResponseFormatTextJSONSchemaConfigParam{
314326 HistoricalComputerSchema, Name: "historical_computer",
315327 ), Description: openai.String("Notable information about a computer"),
328 Schema: schema,
329 Strict: openai.Bool(true),
330 },
331 },
316 },332 },
317 })333 })
318 if err != nil {334 if err != nil {
321 337
322 // extract into a well-typed struct338 // extract into a well-typed struct
323 var historicalComputer HistoricalComputer339 var historicalComputer HistoricalComputer
324340 _ = json.Unmarshal([]byte(response.OutputText()), &historicalComputer) if err := json.Unmarshal([]byte(response.OutputText()), &historicalComputer); err != nil {
341 panic(err)
342 }
325 343
326 historicalComputer.Name344 historicalComputer.Name
327 historicalComputer.Origin.YearBuilt345 historicalComputer.Origin.YearBuilt
332}350}
333```351```
334 352
335353> See the [full structured outputs example](./examples/responses-structured-outputs/main.go)> See the [full structured outputs example](./examples/structured-outputs/main.go)
336 354
337</details>355</details>
338 356