Skip to content
Closed
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
75 changes: 15 additions & 60 deletions libs/langchain-core/src/language_models/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Tiktoken, TiktokenModel } from "js-tiktoken/lite";
import type { ZodType as ZodTypeV3 } from "zod/v3";
import type { $ZodType as ZodTypeV4 } from "zod/v4/core";

import { type BaseCache, InMemoryCache } from "../caches/base.js";
import {
Expand All @@ -26,6 +24,7 @@ import {
InteropZodObject,
InteropZodType,
} from "../utils/types/zod.js";
import { type AIMessage, type AIMessageChunk } from "../messages/ai.js";

// https://www.npmjs.com/package/js-tiktoken

Expand Down Expand Up @@ -280,6 +279,8 @@ export type BaseLanguageModelInput =
| string
| BaseMessageLike[];

export type AnyAIMessage = AIMessage | AIMessageChunk;

export type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;

export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =
Expand Down Expand Up @@ -560,49 +561,15 @@ export abstract class BaseLanguageModel<
throw new Error("Use .toJSON() instead");
}

withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
schema:
| ZodTypeV3<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<false>
): Runnable<BaseLanguageModelInput, RunOutput>;

withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
schema:
| ZodTypeV3<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<true>
): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;

withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
schema:
| ZodTypeV4<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
withStructuredOutput?<Output extends Record<string, unknown>>(
schema: InteropZodType<Output> | JSONSchema,
config?: StructuredOutputMethodOptions<false>
): Runnable<BaseLanguageModelInput, RunOutput>;
): BaseLanguageModel<Output, CallOptions>;

withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
schema:
| ZodTypeV4<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
config?: StructuredOutputMethodOptions<true>
): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;
withStructuredOutput?<Output extends Record<string, unknown>>(
schema: InteropZodType<Output> | JSONSchema,
config: StructuredOutputMethodOptions<true>
): BaseLanguageModel<{ raw: AnyAIMessage; parsed: Output }, CallOptions>;

/**
* Model wrapper that returns outputs formatted to match the given schema.
Expand All @@ -615,26 +582,14 @@ export abstract class BaseLanguageModel<
* @param {string} name The name of the function to call.
* @param {"functionCalling" | "jsonMode"} [method=functionCalling] The method to use for getting the structured output. Defaults to "functionCalling".
* @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.
* @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
* @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: AnyAIMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
*/
withStructuredOutput?<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = Record<string, any>
>(
schema:
| InteropZodType<RunOutput>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| Record<string, any>,
withStructuredOutput?<Output extends Record<string, unknown>>(
schema: InteropZodType<Output> | JSONSchema,
config?: StructuredOutputMethodOptions<boolean>
):
| Runnable<BaseLanguageModelInput, RunOutput>
| Runnable<
BaseLanguageModelInput,
{
raw: BaseMessage;
parsed: RunOutput;
}
>;
| BaseLanguageModel<Output, CallOptions>
| BaseLanguageModel<{ raw: AnyAIMessage; parsed: Output }, CallOptions>;
}

/**
Expand Down
Loading
Loading