@claritylabs/cl-sdk 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -89,6 +89,8 @@ type GenerateObject<T> = (params: {
89
89
 
90
90
  Works with any provider: Anthropic, OpenAI, Google, Mistral, Bedrock, Azure, Ollama, etc. You write the adapter once; the SDK calls it throughout the pipeline.
91
91
 
92
+ > **Strict structured output compatibility:** The SDK automatically transforms Zod schemas before passing them to `generateObject` — converting `.optional()` fields to `.nullable()` so all properties appear in the JSON Schema `required` array. This ensures compatibility with providers like OpenAI that enforce strict structured output validation. No adapter changes needed on your end.
93
+
92
94
  ### Extraction Pipeline
93
95
 
94
96
  The extraction system uses a **coordinator/worker pattern** — a coordinator agent plans the work, specialized extractor agents execute in parallel, and a review loop ensures completeness.
@@ -592,13 +594,40 @@ import type {
592
594
 
593
595
  ```typescript
594
596
  import {
595
- withRetry, // Exponential backoff with jitter (5 retries, 2–32s) for rate limits
596
- pLimit, // Concurrency limiter for parallel async tasks
597
- sanitizeNulls, // Recursively convert null → undefined (for database compatibility)
598
- stripFences, // Remove markdown code fences from LLM JSON responses
597
+ withRetry, // Exponential backoff with jitter (5 retries, 2–32s) for rate limits + transient errors
598
+ pLimit, // Concurrency limiter for parallel async tasks
599
+ sanitizeNulls, // Recursively convert null → undefined (for database compatibility)
600
+ stripFences, // Remove markdown code fences from LLM JSON responses
601
+ safeGenerateObject, // generateObject wrapper with retry, schema strictification, and fallback
602
+ toStrictSchema, // Convert .optional() → .nullable() for strict structured output APIs
599
603
  } from "@claritylabs/cl-sdk";
600
604
  ```
601
605
 
606
+ ### Schema Compatibility
607
+
608
+ The SDK automatically handles schema compatibility with strict structured output APIs (like OpenAI). Two key mechanisms:
609
+
610
+ **`toStrictSchema(schema)`** — Recursively transforms Zod schemas so `.optional()` properties become `.nullable()`. This ensures all properties appear in the JSON Schema `required` array, which OpenAI requires. Applied automatically inside the pipeline — you don't need to call this yourself unless building custom pipelines.
611
+
612
+ **`safeGenerateObject(generateObject, params, options?)`** — Wraps a `generateObject` call with:
613
+ 1. Automatic schema strictification via `toStrictSchema`
614
+ 2. Retry on schema validation errors and transient API failures
615
+ 3. Optional fallback value when all retries are exhausted
616
+
617
+ ```typescript
618
+ import { safeGenerateObject } from "@claritylabs/cl-sdk";
619
+
620
+ const { object, usage } = await safeGenerateObject(
621
+ myGenerateObject,
622
+ { prompt: "...", schema: MySchema, maxTokens: 1024 },
623
+ {
624
+ fallback: { field: "default" }, // Return this if all retries fail
625
+ maxRetries: 2, // Schema validation retries (default: 1)
626
+ log: async (msg) => console.log(msg),
627
+ },
628
+ );
629
+ ```
630
+
602
631
  ## Development
603
632
 
604
633
  ```bash