Skip to content

GH-6007: Unwrap Optional values in Neo4jChatMemoryRepository metadata before saving#6190

Open
suryateja-g13 wants to merge 1 commit into
spring-projects:mainfrom
suryateja-g13:gh-6007-neo4j-optional-metadata
Open

GH-6007: Unwrap Optional values in Neo4jChatMemoryRepository metadata before saving#6190
suryateja-g13 wants to merge 1 commit into
spring-projects:mainfrom
suryateja-g13:gh-6007-neo4j-optional-metadata

Conversation

@suryateja-g13
Copy link
Copy Markdown
Contributor

Summary

  • Fixes Neo4jChatMemoryRepository.saveAll() to unwrap java.util.Optional values from the AssistantMessage metadata map before passing them to the Neo4j driver
  • When using chat models whose SDKs return Optional-typed fields in response objects (e.g. the OpenAI Java SDK's refusal and toolCalls fields on ChatCompletionMessage), those Optional instances end up in the AssistantMessage metadata map
  • The Neo4j driver's Values.value() does not know how to convert java.util.Optional and throws ClientException: Unable to convert java.util.Optional to Neo4j Value
  • Fix adds a private sanitizeMetadata() helper that copies the metadata map while unwrapping Optional values — present Optionals yield their wrapped value, empty Optionals are dropped (treated as absent)

Test plan

  • Module compiles cleanly: ./mvnw -pl memory/repository/spring-ai-model-chat-memory-repository-neo4j test
  • Integration tests in Neo4jChatMemoryRepositoryIT require a live Neo4j instance; unit verification: the change is a straightforward instanceof Optional<?> pattern-match applied before handing off to the driver

Fixes: #6007

…ore saving

When using Neo4jChatMemoryRepository with chat models whose SDK returns
Optional-typed fields in response objects (e.g. OpenAI Java SDK's refusal
and toolCalls fields), those Optional instances are stored in the
AssistantMessage metadata map and then passed directly to the Neo4j driver
via Values.value(), which does not know how to convert java.util.Optional
and throws ClientException.

Fix: add a sanitizeMetadata() helper that copies the metadata map while
unwrapping any Optional values — present Optionals yield their wrapped value,
empty Optionals are dropped entirely. This keeps the metadata neo4j-compatible
without requiring changes to how individual model SDKs populate metadata.

Fixes: spring-projects#6007

Signed-off-by: Gorre Surya <suryateja.g13@gmail.com>
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.

Neo4jChatMemoryRepository throws Unable to convert java.util.Optional to Neo4j Value when saving messages with OpenAI

1 participant