Initializes the manager with optional configuration.
Optional logger: ILoggerLogger instance for debugging
Generates structured output conforming to the given schema.
Generation options including prompt and schema
Promise resolving to the generation result
If generation fails after all retries
const result = await manager.generate<Person>({
prompt: 'Extract person info from: John Doe, 30, john@example.com',
schema: personSchema,
schemaName: 'Person',
strict: true,
});
if (result.success) {
console.log(result.data.name); // Type-safe access
}
Generates parallel function/tool calls.
This method enables the LLM to call multiple functions in a single response, useful for parallel data fetching or multi-step operations.
Function call options
Promise resolving to function call results
const result = await manager.generateFunctionCalls({
prompt: 'Get the weather in Paris and London',
functions: [weatherFunction],
maxParallelCalls: 10,
});
// Execute all calls in parallel
await Promise.all(result.calls.map(async call => {
const fn = functions.find(f => f.name === call.functionName);
if (fn?.handler) {
call.executionResult = await fn.handler(call.arguments);
}
}));
Extracts structured entities from unstructured text.
Useful for NER, data extraction, and information retrieval tasks.
Extraction options
Promise resolving to extraction results
const result = await manager.extractEntities<Person>({
text: 'John Doe (john@example.com) met Jane Smith (jane@example.com)',
entitySchema: personSchema,
taskName: 'PersonExtraction',
extractAll: true,
});
result.entities.forEach(person => {
console.log(`Found: ${person.name} - ${person.email}`);
});
Validates data against a JSON Schema.
Data to validate
Schema to validate against
Optional strict: booleanWhether to fail on additional properties
Array of validation issues (empty if valid)
const issues = manager.validate(
{ name: 'John', age: -5 },
personSchema,
true
);
if (issues.length > 0) {
issues.forEach(issue => {
console.log(`${issue.path}: ${issue.message}`);
});
}
Parses JSON string with error recovery.
Attempts to extract valid JSON from potentially malformed output, handling common LLM output issues like:
String to parse
Parsed object or null if parsing fails
// Handles markdown-wrapped JSON
const data = manager.parseJSON('```json\n{"name": "John"}\n```');
// Returns: { name: 'John' }
// Handles trailing commas
const data2 = manager.parseJSON('{"a": 1, "b": 2,}');
// Returns: { a: 1, b: 2 }
Determines the best strategy for a given provider/model.
LLM provider ID
Model ID
Schema to generate for
Recommended strategy
Registers a custom schema for reuse.
Schema name for reference
Schema definition
manager.registerSchema('Address', {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
country: { type: 'string' },
postalCode: { type: 'string' },
},
required: ['street', 'city', 'country'],
});
// Use in other schemas via $ref
const orderSchema = {
type: 'object',
properties: {
shippingAddress: { $ref: '#/$defs/Address' },
},
};
Gets a registered schema by name.
Schema name
Schema or undefined if not found
Gets statistics about structured output operations.
Current statistics
Interface for the Structured Output Manager.
The Structured Output Manager provides a unified API for generating LLM outputs that conform to predefined JSON Schemas. It handles:
Example
Example