Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openai,core): Make OpenAI withStructuredOutput typing compatible with other models #6957

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion langchain-core/src/language_models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =
name?: string;
method?: "functionCalling" | "jsonMode" | "jsonSchema" | string;
includeRaw?: IncludeRaw;
/** Whether to use strict mode. Currently only supported by OpenAI models. */
strict?: boolean;
};

/** @deprecated Use StructuredOutputMethodOptions instead */
Expand Down Expand Up @@ -514,7 +516,6 @@ export abstract class BaseLanguageModel<
withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
>(
schema:
| z.ZodType<RunOutput>
Expand Down
6 changes: 5 additions & 1 deletion langchain-core/src/language_models/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,6 @@ export abstract class BaseChatModel<
withStructuredOutput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
>(
outputSchema:
| z.ZodType<RunOutput>
Expand Down Expand Up @@ -845,6 +844,11 @@ export abstract class BaseChatModel<
`Chat model must implement ".bindTools()" to use withStructuredOutput.`
);
}
if (config?.strict) {
throw new Error(
`"strict" mode is not supported for this model by default.`
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schema: z.ZodType<RunOutput> | Record<string, any> = outputSchema;
const name = config?.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ChatPromptTemplate } from "@langchain/core/prompts";
import { concat } from "@langchain/core/utils/stream";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatOpenAI } from "@langchain/openai";
import { BedrockChat as BedrockChatWeb } from "../bedrock/web.js";
import { TavilySearchResults } from "../../tools/tavily_search.js";

Expand Down Expand Up @@ -531,3 +532,31 @@ test("Streaming tool calls with Anthropic", async () => {
expect(finalChunk?.tool_calls?.[0].name).toBe("weather_tool");
expect(finalChunk?.tool_calls?.[0].args?.city).toBeDefined();
});

test("withStructuredOutput result should be compatible with OpenAI typing", async () => {
const testSchema = z.object({
thinking_process: z
.string()
.describe(
"Think before generating variants and put your reasoning here."
),
variants: z
.array(
z.object({
name: z.string(),
value: z.string(),
})
)
.describe("Variants of the input"),
});

const _prepareClient = () => {
if (Math.random() > 0.5) {
return new ChatOpenAI();
}

return new BedrockChatWeb();
};

_prepareClient().withStructuredOutput(testSchema);
});
4 changes: 1 addition & 3 deletions libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ function _convertChatOpenAIToolTypeToOpenAITool(
return _convertToOpenAITool(tool, fields);
}

// TODO: Use the base structured output options param in next breaking release.
export interface ChatOpenAIStructuredOutputMethodOptions<
IncludeRaw extends boolean
> extends StructuredOutputMethodOptions<IncludeRaw> {
Expand Down Expand Up @@ -1940,7 +1941,6 @@ export class ChatOpenAI<
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| StructuredOutputMethodParams<RunOutput, false>
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
Expand All @@ -1952,7 +1952,6 @@ export class ChatOpenAI<
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| StructuredOutputMethodParams<RunOutput, true>
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
Expand All @@ -1964,7 +1963,6 @@ export class ChatOpenAI<
RunOutput extends Record<string, any> = Record<string, any>
>(
outputSchema:
| StructuredOutputMethodParams<RunOutput, boolean>
| z.ZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
Expand Down
Loading