fix: The createObjectIfPossible method is unreliable on some LLMs#1386
Closed
johnsonr wants to merge 2 commits intoembabel:mainfrom
Closed
fix: The createObjectIfPossible method is unreliable on some LLMs#1386johnsonr wants to merge 2 commits intoembabel:mainfrom
createObjectIfPossible method is unreliable on some LLMs#1386johnsonr wants to merge 2 commits intoembabel:mainfrom
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.
igordayen
reviewed
Feb 6, 2026
| val node = parser.codec.readTree<com.fasterxml.jackson.databind.JsonNode>(parser) | ||
|
|
||
| val failureNode = node.get("failure") | ||
| if (failureNode != null && !failureNode.isNull) { |
Contributor
There was a problem hiding this comment.
maybe consider more kotlin idiomati such as failureNode?.isNull
| else -> | ||
| "Invalid success object: ${e.message}" | ||
| } | ||
| MaybeReturn(success = null, failure = errorMessage) |
| } | ||
|
|
||
| // Neither success nor failure provided | ||
| return MaybeReturn(success = null, failure = "Neither success nor failure field provided") |
Contributor
There was a problem hiding this comment.
unit test for this scenarion would be good to have
igordayen
reviewed
Feb 6, 2026
| * ``` | ||
| * when they don't have enough information to populate all required fields. | ||
| */ | ||
| internal class MaybeReturnDeserializer<T>( |
Contributor
There was a problem hiding this comment.
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
Contributor
|
@johnsonr: applying similar fix upon createObjectIfPossible refactoring. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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