My agentic slop goes here. Not intended for anyone else!
1let src = Logs.Src.create "claude.structured_output" ~doc:"Structured output"
2module Log = (val Logs.src_log src : Logs.LOG)
3
4type t = {
5 json_schema : Jsont.json;
6}
7
8let json_to_string json =
9 match Jsont_bytesrw.encode_string' Jsont.json json with
10 | Ok str -> str
11 | Error err -> failwith (Jsont.Error.to_string err)
12
13let of_json_schema schema =
14 Log.debug (fun m -> m "Created output format from JSON schema: %s"
15 (json_to_string schema));
16 { json_schema = schema }
17
18let json_schema t = t.json_schema
19
20(* Codec for serializing structured output format *)
21let jsont : t Jsont.t =
22 Jsont.Object.map ~kind:"StructuredOutput"
23 (fun json_schema -> {json_schema})
24 |> Jsont.Object.mem "jsonSchema" Jsont.json ~enc:(fun t -> t.json_schema)
25 |> Jsont.Object.finish
26
27let to_json t =
28 match Jsont.Json.encode jsont t with
29 | Ok json -> json
30 | Error msg -> failwith ("Structured_output.to_json: " ^ msg)
31
32let of_json json =
33 match Jsont.Json.decode jsont json with
34 | Ok t -> t
35 | Error msg -> raise (Invalid_argument ("Structured_output.of_json: " ^ msg))
36
37let pp fmt t =
38 let schema_str =
39 match Jsont_bytesrw.encode_string' ~format:Jsont.Minify Jsont.json t.json_schema with
40 | Ok s -> s
41 | Error err -> Jsont.Error.to_string err
42 in
43 let truncated =
44 if String.length schema_str > 100 then
45 String.sub schema_str 0 97 ^ "..."
46 else
47 schema_str
48 in
49 Fmt.pf fmt "@[<2>StructuredOutput { schema = %s }@]" truncated