-
Notifications
You must be signed in to change notification settings - Fork 288
fix: The createObjectIfPossible method is unreliable on some LLMs
#1386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…method is unreliable on some LLMs When createObjectIfPossible receives a malformed MaybeReturn response from an LLM where the success field is present but contains partial/invalid data (e.g., missing required fields or null values for non-nullable fields), the deserialization fails with MissingKotlinParameterException. This test reproduces the issue to verify that the fix properly handles these edge cases by returning a failure Result instead of throwing an exception.
When createObjectIfPossible receives a malformed MaybeReturn response from an LLM where the success field is present but contains invalid data (e.g., missing required fields, null values for non-nullable fields), the system now treats this as a failure case rather than throwing a deserialization exception. The fix implements a custom Jackson deserializer (MaybeReturnDeserializer) that: - Catches MissingKotlinParameterException and other deserialization errors during success field parsing - Converts these errors into a failure MaybeReturn with a descriptive message - Returns a proper Result.failure instead of propagating the exception This makes the system more robust when dealing with LLMs that don't follow the "never return partial structures" instruction in the maybe prompt template.
| val node = parser.codec.readTree<com.fasterxml.jackson.databind.JsonNode>(parser) | ||
|
|
||
| val failureNode = node.get("failure") | ||
| if (failureNode != null && !failureNode.isNull) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe consider more kotlin idiomati such as failureNode?.isNull
| else -> | ||
| "Invalid success object: ${e.message}" | ||
| } | ||
| MaybeReturn(success = null, failure = errorMessage) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
| } | ||
|
|
||
| // Neither success nor failure provided | ||
| return MaybeReturn(success = null, failure = "Neither success nor failure field provided") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit test for this scenarion would be good to have
| * ``` | ||
| * when they don't have enough information to populate all required fields. | ||
| */ | ||
| internal class MaybeReturnDeserializer<T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ChatClientLlmOperations is getting larger. candidate perhaps to consider extension functions file (lower camel case) and if access to private members is allowed from extension file
Summary
Automated fix for #637
Changes
embabel-agent-api/src/main/kotlin/com/embabel/agent/spi/support/springai/ChatClientLlmOperations.ktembabel-agent-api/src/test/kotlin/com/embabel/agent/spi/support/ChatClientLlmOperationsTest.ktDescription
Fixed MaybeReturn deserialization issue by implementing a custom Jackson deserializer that catches exceptions when parsing the success field and treats them as failures instead of throwing exceptions. All 2417 tests pass.
Generated by Blackmaw