Replies: 1 comment 1 reply
-
Hey, @sibbl! I'm here to help you out with your coding questions and bug fixes. Let's tackle this issue together! To set the
Here's how you can modify your code to achieve this: import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate
} from "@langchain/core/prompts";
import { SystemMessage } from "@langchain/core/messages";
const promptTemplate = ChatPromptTemplate.fromMessages([
new SystemMessage("Be friendly to the user"),
HumanMessagePromptTemplate.fromTemplate("{userInput}")
]);
type SchemaBaseType = {
type: "one" | "two";
label: string;
};
type SchemaOneType = SchemaBaseType & {
type: "one";
one: string | null;
};
type SchemaTwoType = SchemaBaseType & {
type: "two";
two: string | null;
};
type SchemaType = SchemaOneType | SchemaTwoType;
type ResultType = {
elements: SchemaType[];
};
async function getCompletionAsync(): Promise<ResultType> {
const SchemaBase = z
.object({
type: z.enum(["one", "two"]),
label: z.string()
})
.strict();
const SchemaOne = SchemaBase.extend({
type: z.literal("one"),
one: z.union([z.string(), z.null()])
});
const SchemaTwo = SchemaBase.extend({
type: z.literal("two"),
two: z.union([z.string(), z.null()])
});
const schema = z.object({
elements: z.array(z.discriminatedUnion("type", [SchemaOne, SchemaTwo]))
});
// Convert Zod schema to JSON schema with $refStrategy: "none"
const jsonSchema = zodToJsonSchema(schema, {
$refStrategy: "none"
});
const model = new ChatOpenAI();
const structuredLlm = await model.withStructuredOutput(jsonSchema, {
strict: true
});
const chain = promptTemplate.pipe(structuredLlm);
const result = await chain.invoke({
userInput: "hello world"
});
return result as ResultType; // Cast the result to ResultType
} By converting the Zod schema to JSON schema with This approach allows you to use strict mode with a complex JSON schema while maintaining typed results. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
400 Invalid schema for function 'extract': In context=('properties', 'elements', 'items', 'anyOf', '1', 'properties', 'label'), reference can only point to definitions defined at the top level of the schema.
zodToJsonSchema
is used, which accepts a$refStrategy: "none"
option to avoid refs being usedBy using
I can basically get it working, but I loose the typed result as
model.withStructuredOutput
then returnsRecord<string, any>
and notResultType
anymore.System Info
Windows, Node 20.16.0, npm 10.8.1
Beta Was this translation helpful? Give feedback.
All reactions