@claritylabs/cl-sdk 0.7.2 → 0.7.4
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 +33 -4
- package/dist/index.d.mts +179 -168
- package/dist/index.d.ts +179 -168
- package/dist/index.js +1118 -980
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1117 -980
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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,
|
|
596
|
-
pLimit,
|
|
597
|
-
sanitizeNulls,
|
|
598
|
-
stripFences,
|
|
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
|