Open ptesny opened 3 months ago
Hey @ptesny! Great to see you diving into another challenge. Let's see what we can do about this one.
Thank you for providing detailed information about the issue you're facing. The error TypeError: Cannot read properties of undefined (reading 'additional_kwargs')
suggests that the object expected to have the additional_kwargs
property is undefined
. This issue might be arising from how the withStructuredOutput
method is being used or how the response from the model is being handled.
Given the context and the code snippet, the issue might not be directly visible in the provided code. However, the error typically occurs when trying to access a property on an undefined object. In this case, it seems related to the handling of the response from the model, particularly when dealing with the includeRaw
option.
To resolve this issue, ensure that every part of the response object where additional_kwargs
is accessed is properly checked for undefined or null values. This can be done by optional chaining (?.
) or by explicitly checking the object before accessing its properties.
Here's a revised approach to safely access additional_kwargs
, applying optional chaining:
console.log(JSON.stringify(includeRawResponse?.raw?.kwargs?.additional_kwargs, null, 2));
If the issue persists, it might be due to how the withStructuredOutput
method is implemented or called. Since the direct cause is not visible in the snippet, consider the following steps:
Review the withStructuredOutput
Method: Ensure that the method correctly handles the options object and that it properly integrates with the underlying API calls. There might be a mismatch in how options are passed or handled within the library.
Check for Library Updates: There might be a bug in the version of @langchain/mistralai
or @mistralai/mistralai
you are using. Check the repositories or package management listings for any updates or bug fixes related to this issue.
Debugging and Logging: Add more logging around the area where additional_kwargs
is expected to be accessed. This can help identify if the issue is with the response format or with how the response is being processed.
If the issue is not resolved by the above steps, consult the official documentation or reach out to the support channels for @langchain/mistralai
and @mistralai/mistralai
. There might be known issues or additional configuration steps required for your specific use case.
Remember, the key is to ensure that you're safely accessing properties on objects that might not always be defined, especially when dealing with asynchronous operations or external API responses.
For more context, the additional_kwargs
property in the @langchain/mistralai
wrapper is used to provide extra context or metadata to the language model (LLM) about the specific tool or function being invoked. In the provided context, it is used within the includeRaw
option to include additional details about the tool being called, specifically when using structured output with a schema.
In the example, the additional_kwargs
contains a tool_calls
array, which includes details about the tool being invoked. Each item in the tool_calls
array has an id
, type
, and a function
object. The function
object includes the name
of the tool (in this case, "calculator") and the arguments
passed to it, serialized as a JSON string. This structure allows the model to understand that it is performing a calculation and provides it with the necessary details (operation type and numbers involved) to execute the calculation and return the structured output.
This mechanism is particularly useful for applications that require the model to perform specific tasks or operations based on structured input, allowing developers to leverage the model's capabilities in a more controlled and semantically rich manner.
You can find more details about this in the examples/src/models/chat/chat_mistralai_wsa.ts file in the LangChain JS codebase.
I hope this helps! If you have any more questions or need further clarification, feel free to ask.
I have tried to use the code snippet from https://www.npmjs.com/package/@langchain/mistralai#streaming; it is quite different from the code samples above
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
Trying to use @langchain/mistralai as a wrapper over the @mistralai/mistralai nodejs client. Worth mentioning, the @mistralai/mistralai client is working fine.... I have tried to align the versions of @langchain/core using the caret notation in package.json; I've tried to use
ChatMessage
,HumanMessage
andBaseMessage
as well. Always the same error. Thank youSystem Info
package.json