@@ -45,39 +45,53 @@ export class LangChainProvider extends AIProvider {
4545 * Invoke the LangChain model with an array of messages.
4646 */
4747 async invokeModel ( messages : LDMessage [ ] ) : Promise < ChatResponse > {
48- // Convert LDMessage[] to LangChain messages
49- const langchainMessages = LangChainProvider . convertMessagesToLangChain ( messages ) ;
50-
51- // Get the LangChain response
52- const response : AIMessage = await this . _llm . invoke ( langchainMessages ) ;
53-
54- // Generate metrics early (assumes success by default)
55- const metrics = LangChainProvider . createAIMetrics ( response ) ;
56-
57- // Extract text content from the response
58- let content : string = '' ;
59- if ( typeof response . content === 'string' ) {
60- content = response . content ;
61- } else {
62- // Log warning for non-string content (likely multimodal)
63- this . logger ?. warn (
64- `Multimodal response not supported, expecting a string. Content type: ${ typeof response . content } , Content:` ,
65- JSON . stringify ( response . content , null , 2 ) ,
66- ) ;
67- // Update metrics to reflect content loss
68- metrics . success = false ;
69- }
48+ try {
49+ // Convert LDMessage[] to LangChain messages
50+ const langchainMessages = LangChainProvider . convertMessagesToLangChain ( messages ) ;
51+
52+ // Get the LangChain response
53+ const response : AIMessage = await this . _llm . invoke ( langchainMessages ) ;
54+
55+ // Generate metrics early (assumes success by default)
56+ const metrics = LangChainProvider . createAIMetrics ( response ) ;
57+
58+ // Extract text content from the response
59+ let content : string = '' ;
60+ if ( typeof response . content === 'string' ) {
61+ content = response . content ;
62+ } else {
63+ // Log warning for non-string content (likely multimodal)
64+ this . logger ?. warn (
65+ `Multimodal response not supported, expecting a string. Content type: ${ typeof response . content } , Content:` ,
66+ JSON . stringify ( response . content , null , 2 ) ,
67+ ) ;
68+ // Update metrics to reflect content loss
69+ metrics . success = false ;
70+ }
7071
71- // Create the assistant message
72- const assistantMessage : LDMessage = {
73- role : 'assistant' ,
74- content,
75- } ;
72+ // Create the assistant message
73+ const assistantMessage : LDMessage = {
74+ role : 'assistant' ,
75+ content,
76+ } ;
7677
77- return {
78- message : assistantMessage ,
79- metrics,
80- } ;
78+ return {
79+ message : assistantMessage ,
80+ metrics,
81+ } ;
82+ } catch ( error ) {
83+ this . logger ?. warn ( 'LangChain model invocation failed:' , error ) ;
84+
85+ return {
86+ message : {
87+ role : 'assistant' ,
88+ content : '' ,
89+ } ,
90+ metrics : {
91+ success : false ,
92+ } ,
93+ } ;
94+ }
8195 }
8296
8397 /**
@@ -87,29 +101,46 @@ export class LangChainProvider extends AIProvider {
87101 messages : LDMessage [ ] ,
88102 responseStructure : Record < string , unknown > ,
89103 ) : Promise < StructuredResponse > {
90- // Convert LDMessage[] to LangChain messages
91- const langchainMessages = LangChainProvider . convertMessagesToLangChain ( messages ) ;
92-
93- // Get the LangChain response
94- const response = await this . _llm
95- . withStructuredOutput ( responseStructure )
96- . invoke ( langchainMessages ) ;
97-
98- // Using structured output doesn't support metrics
99- const metrics = {
100- success : true ,
101- usage : {
102- total : 0 ,
103- input : 0 ,
104- output : 0 ,
105- } ,
106- } ;
104+ try {
105+ // Convert LDMessage[] to LangChain messages
106+ const langchainMessages = LangChainProvider . convertMessagesToLangChain ( messages ) ;
107+
108+ // Get the LangChain response
109+ const response = await this . _llm
110+ . withStructuredOutput ( responseStructure )
111+ . invoke ( langchainMessages ) ;
112+
113+ // Using structured output doesn't support metrics
114+ const metrics = {
115+ success : true ,
116+ usage : {
117+ total : 0 ,
118+ input : 0 ,
119+ output : 0 ,
120+ } ,
121+ } ;
107122
108- return {
109- data : response ,
110- rawResponse : JSON . stringify ( response ) ,
111- metrics,
112- } ;
123+ return {
124+ data : response ,
125+ rawResponse : JSON . stringify ( response ) ,
126+ metrics,
127+ } ;
128+ } catch ( error ) {
129+ this . logger ?. warn ( 'LangChain structured model invocation failed:' , error ) ;
130+
131+ return {
132+ data : { } ,
133+ rawResponse : '' ,
134+ metrics : {
135+ success : false ,
136+ usage : {
137+ total : 0 ,
138+ input : 0 ,
139+ output : 0 ,
140+ } ,
141+ } ,
142+ } ;
143+ }
113144 }
114145
115146 /**
0 commit comments