Open saschTa opened 3 weeks ago
Hello, @saschTa! How can I assist you today?
The issue you're experiencing with the withStructuredOutput
method not enforcing JSON output when a system message is included in the template might be related to how the system message is processed by the language model. Here are a few points to consider:
System Message Influence: The system message can influence the model's behavior significantly. If the system message is not clear or if it conflicts with the structured output requirements, it might lead to inconsistent results. In your case, the system message instructs the model to always answer in a JSON structure, but it seems this instruction might not be effectively guiding the model.
Method Configuration: Ensure that the method
option is correctly set to "json_mode"
, which is intended to enforce JSON output. However, if the system message is not aligned with this mode, it might not work as expected.
Schema Validation: The z.object
schema is used to validate the output. If the output does not match the schema, it should ideally throw an OutputParserException
. However, if the output is not even in JSON format, the parsing might fail silently, resulting in undefined
values.
Debugging Steps:
Potential Bug: If the behavior significantly deviates from the expected functionality as described in the documentation, it might be a bug in the LangChain.js library. In such cases, further investigation or a workaround might be necessary until a fix is provided.
The behavior you're observing, where the output is not enforced as JSON and the invoke
call returns undefined
, aligns with known issues where the output does not match the expected JSON structure [1][2].
To continue talking to Dosu, mention @dosu.
CC @afirstenberg @bracesproul
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
When firing above described request, I get very random results back. Json is not enforced and the invoke call does not throw but returns undefined (since it could not parse the LLMs output),
When omitting the system message it works well. Does a system message interfere with structured output in this case?
One remark when there is no system message in this example, the output is always enforced and raw response is always empty. So the behaviour does not really match documentation. Update: I also found out, that the zod enforcement does not throw an error if it cant parse model output. My model returned null for a property which was defined as optional and not nullable. That caused the parsed response to be undefined.
System Info