55 type BaseMessageLike ,
66 HumanMessage ,
77 coerceMessageLikeToMessage ,
8- AIMessageChunk ,
98 isAIMessageChunk ,
109 isBaseMessage ,
1110 isAIMessage ,
@@ -29,13 +28,14 @@ import {
2928 type BaseLanguageModelCallOptions ,
3029 type BaseLanguageModelInput ,
3130 type BaseLanguageModelParams ,
31+ type AnyAIMessage ,
3232} from "./base.js" ;
3333import {
3434 CallbackManager ,
3535 type CallbackManagerForLLMRun ,
3636 type Callbacks ,
3737} from "../callbacks/manager.js" ;
38- import type { RunnableConfig } from "../runnables/config.js" ;
38+ import { mergeConfigs , type RunnableConfig } from "../runnables/config.js" ;
3939import type { BaseCache } from "../caches/base.js" ;
4040import {
4141 StructuredToolInterface ,
@@ -180,18 +180,18 @@ export type BindToolsInput =
180180 | RunnableToolLike
181181 | StructuredToolParams ;
182182
183- export type ChatModelOutputParser < T = unknown > = Runnable < BaseMessage , T > ;
184- export type InferChatModelOutputParser < TOutput > = TOutput extends BaseMessage
185- ? ChatModelOutputParser < TOutput >
186- : undefined ;
183+ export type ChatModelOutputParser < T = unknown > = Runnable < AnyAIMessage , T > ;
184+ export type InferChatModelOutputParser < TOutput > = TOutput extends AnyAIMessage
185+ ? undefined
186+ : ChatModelOutputParser < TOutput > ;
187187
188188/**
189189 * Base class for chat models. It extends the BaseLanguageModel class and
190190 * provides methods for generating chat based on input messages.
191191 */
192192export abstract class BaseChatModel <
193193 CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions ,
194- TOutput = BaseMessage
194+ TOutput = AnyAIMessage
195195> extends BaseLanguageModel < TOutput , CallOptions > {
196196 // Backwards compatibility since fields have been moved to RunnableConfig
197197 declare ParsedCallOptions : Omit <
@@ -214,11 +214,8 @@ export abstract class BaseChatModel<
214214
215215 protected _combineCallOptions (
216216 additionalOptions ?: Partial < CallOptions >
217- ) : CallOptions {
218- return {
219- ...this . defaultOptions ,
220- ...additionalOptions ,
221- } ;
217+ ) : Partial < CallOptions > {
218+ return mergeConfigs ( this . defaultOptions , additionalOptions ) ;
222219 }
223220
224221 protected async _parseOutput ( output : BaseMessage ) : Promise < TOutput > {
@@ -237,7 +234,9 @@ export abstract class BaseChatModel<
237234 ) : [ RunnableConfig , this[ "ParsedCallOptions" ] ] {
238235 // For backwards compat, keep `signal` in both runnableConfig and callOptions
239236 const [ runnableConfig , callOptions ] =
240- super . _separateRunnableConfigFromCallOptions ( options ) ;
237+ super . _separateRunnableConfigFromCallOptions (
238+ this . _combineCallOptions ( options )
239+ ) ;
241240 ( callOptions as this[ "ParsedCallOptions" ] ) . signal = runnableConfig . signal ;
242241 return [ runnableConfig , callOptions as this[ "ParsedCallOptions" ] ] ;
243242 }
@@ -250,10 +249,7 @@ export abstract class BaseChatModel<
250249 * matching the provider's specific tool schema.
251250 * @param kwargs Any additional parameters to bind.
252251 */
253- abstract bindTools ?(
254- tools : BindToolsInput [ ] ,
255- options ?: Partial < CallOptions >
256- ) : BaseChatModel < CallOptions , TOutput > ;
252+ bindTools ?( tools : BindToolsInput [ ] , options ?: Partial < CallOptions > ) : this;
257253
258254 /**
259255 * Invokes the chat model with a single input.
@@ -298,9 +294,8 @@ export abstract class BaseChatModel<
298294 } else {
299295 const prompt = BaseChatModel . _convertInputToPromptValue ( input ) ;
300296 const messages = prompt . toChatMessages ( ) ;
301- const combinedOptions = this . _combineCallOptions ( options ) ;
302297 const [ runnableConfig , callOptions ] =
303- this . _separateRunnableConfigFromCallOptionsCompat ( combinedOptions ) ;
298+ this . _separateRunnableConfigFromCallOptionsCompat ( options ) ;
304299
305300 const inheritableMetadata = {
306301 ...runnableConfig . metadata ,
@@ -936,7 +931,7 @@ export abstract class BaseChatModel<
936931 }
937932
938933 /** @internal */
939- protected withOutputParser < TOutput > (
934+ protected withOutputParser < TOutput extends Record < string , unknown > > (
940935 outputParser : ChatModelOutputParser < TOutput >
941936 ) : BaseChatModel < CallOptions , TOutput > {
942937 const Cls = this . constructor as Constructor <
@@ -948,29 +943,29 @@ export abstract class BaseChatModel<
948943 return instance ;
949944 }
950945
951- withStructuredOutput < Output > (
946+ withStructuredOutput < Output extends Record < string , unknown > > (
952947 schema : InteropZodType < Output > | JSONSchema ,
953948 config ?: StructuredOutputMethodOptions < false >
954949 ) : BaseChatModel < CallOptions , Output > ;
955950
956- withStructuredOutput < Output > (
951+ withStructuredOutput < Output extends Record < string , unknown > > (
957952 schema : InteropZodType < Output > | JSONSchema ,
958- config ? : StructuredOutputMethodOptions < true >
959- ) : BaseChatModel < CallOptions , { raw : BaseMessage ; parsed : Output } > ;
953+ config : StructuredOutputMethodOptions < true >
954+ ) : BaseChatModel < CallOptions , { raw : AnyAIMessage ; parsed : Output } > ;
960955
961- withStructuredOutput < Output > (
956+ withStructuredOutput < Output extends Record < string , unknown > > (
962957 schema : InteropZodType < Output > | JSONSchema ,
963958 config ?: StructuredOutputMethodOptions < boolean >
964959 ) :
965960 | BaseChatModel < CallOptions , Output >
966- | BaseChatModel < CallOptions , { raw : BaseMessage ; parsed : Output } > ;
961+ | BaseChatModel < CallOptions , { raw : AnyAIMessage ; parsed : Output } > ;
967962
968- withStructuredOutput < Output > (
963+ withStructuredOutput < Output extends Record < string , unknown > > (
969964 schema : InteropZodType < Output > | JSONSchema ,
970965 config ?: StructuredOutputMethodOptions < boolean >
971966 ) :
972967 | BaseChatModel < CallOptions , Output >
973- | BaseChatModel < CallOptions , { raw : BaseMessage ; parsed : Output } > {
968+ | BaseChatModel < CallOptions , { raw : AnyAIMessage ; parsed : Output } > {
974969 if ( typeof this . bindTools !== "function" ) {
975970 throw new Error (
976971 `Chat model must implement ".bindTools()" to use withStructuredOutput.`
@@ -1022,8 +1017,8 @@ export abstract class BaseChatModel<
10221017 }
10231018
10241019 const llm = this . bindTools ( tools ) ;
1025- const toolMessageParser = RunnableLambda . from < AIMessageChunk , Output > (
1026- ( input : AIMessageChunk ) : Output => {
1020+ const toolMessageParser = RunnableLambda . from < AnyAIMessage , Output > (
1021+ ( input : AnyAIMessage ) : Output => {
10271022 if ( ! input . tool_calls || input . tool_calls . length === 0 ) {
10281023 throw new Error ( "No tool calls found in the response." ) ;
10291024 }
@@ -1046,12 +1041,12 @@ export abstract class BaseChatModel<
10461041 }
10471042
10481043 const rawOutputParser = RunnableLambda . from <
1049- AIMessageChunk ,
1050- { raw : BaseMessage ; parsed : Output }
1044+ AnyAIMessage ,
1045+ { raw : AnyAIMessage ; parsed : Output }
10511046 > (
10521047 async (
1053- input : AIMessageChunk
1054- ) : Promise < { raw : BaseMessage ; parsed : Output } > => {
1048+ input : AnyAIMessage
1049+ ) : Promise < { raw : AnyAIMessage ; parsed : Output } > => {
10551050 return {
10561051 raw : input ,
10571052 parsed : await toolMessageParser . invoke ( input ) ,
0 commit comments