Skip to content

Conversation

@johnsonr
Copy link
Contributor

@johnsonr johnsonr commented Feb 6, 2026

Summary

Automated fix for #637

Changes

  • embabel-agent-api/src/main/kotlin/com/embabel/agent/spi/support/springai/ChatClientLlmOperations.kt
  • embabel-agent-api/src/test/kotlin/com/embabel/agent/spi/support/ChatClientLlmOperationsTest.kt

Description

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

…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.
@johnsonr johnsonr marked this pull request as draft February 6, 2026 09:30
val node = parser.codec.readTree<com.fasterxml.jackson.databind.JsonNode>(parser)

val failureNode = node.get("failure")
if (failureNode != null && !failureNode.isNull) {
Copy link
Contributor

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)
Copy link
Contributor

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")
Copy link
Contributor

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>(
Copy link
Contributor

@igordayen igordayen Feb 6, 2026

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants