My agentic slop goes here. Not intended for anyone else!
1(* Simple example showing structured output with explicit JSON Schema *)
2
3module C = Claude
4
5let () =
6 Logs.set_reporter (Logs_fmt.reporter ());
7 Logs.set_level (Some Logs.Info)
8
9let simple_example env =
10 Printf.printf "\n=== Simple Structured Output Example ===\n\n";
11
12 (* Define a simple schema for a person's info *)
13 let person_schema =
14 let open Jsont in
15 Object ([
16 (("type", Meta.none), String ("object", Meta.none));
17 (("properties", Meta.none), Object ([
18 (("name", Meta.none), Object ([
19 (("type", Meta.none), String ("string", Meta.none))
20 ], Meta.none));
21 (("age", Meta.none), Object ([
22 (("type", Meta.none), String ("integer", Meta.none))
23 ], Meta.none));
24 (("occupation", Meta.none), Object ([
25 (("type", Meta.none), String ("string", Meta.none))
26 ], Meta.none));
27 ], Meta.none));
28 (("required", Meta.none), Array ([
29 String ("name", Meta.none);
30 String ("age", Meta.none);
31 String ("occupation", Meta.none)
32 ], Meta.none))
33 ], Meta.none)
34 in
35
36 let output_format = C.Structured_output.of_json_schema person_schema in
37
38 let options = C.Options.default
39 |> C.Options.with_output_format output_format
40 |> C.Options.with_max_turns 1
41 in
42
43 Printf.printf "Asking Claude to provide structured data...\n\n";
44
45 Eio.Switch.run @@ fun sw ->
46 let process_mgr = Eio.Stdenv.process_mgr env in
47 let client = C.Client.create ~sw ~process_mgr ~options () in
48
49 C.Client.query client
50 "Tell me about a famous computer scientist. Provide their name, age, \
51 and occupation in the exact JSON structure I specified.";
52
53 let messages = C.Client.receive_all client in
54 List.iter (function
55 | C.Message.Result result ->
56 Printf.printf "Response received!\n";
57 (match C.Message.Result.structured_output result with
58 | Some json ->
59 Printf.printf "\nStructured Output:\n%s\n"
60 (Test_json_utils.to_string ~minify:false json)
61 | None ->
62 Printf.printf "No structured output\n")
63 | _ -> ()
64 ) messages
65
66let () =
67 Eio_main.run @@ fun env ->
68 try
69 simple_example env
70 with exn ->
71 Printf.eprintf "Error: %s\n" (Printexc.to_string exn);
72 exit 1