Skip to content

[FLINK-AGENTS][api] Fix Kryo deserialization failure for Event classes with immutable lists#677

Open
avichaym wants to merge 1 commit into
apache:mainfrom
avichaym:fix/kryo-immutable-list-serialization
Open

[FLINK-AGENTS][api] Fix Kryo deserialization failure for Event classes with immutable lists#677
avichaym wants to merge 1 commit into
apache:mainfrom
avichaym:fix/kryo-immutable-list-serialization

Conversation

@avichaym
Copy link
Copy Markdown
Contributor

fixes #673

Wraps list arguments in new ArrayList<>() before passing to setAttr() in ChatRequestEvent, ContextRetrievalResponseEvent, and ToolRequestEvent. Without this, passing List.of(...) crashes Kryo on JDK 17+ with InaccessibleObjectException.

…s with immutable lists

Wrap List arguments in new ArrayList<>() in event constructors so callers
can safely pass List.of(...), Arrays.asList(...), or any immutable list
without hitting a JDK 17+ Kryo InaccessibleObjectException at runtime.

Affected events: ChatRequestEvent (messages), ContextRetrievalResponseEvent
(documents), ToolRequestEvent (tool_calls).

The defensive copy is applied before setAttr() so the stored list is always
a plain mutable ArrayList that Kryo can serialize without reflective access
to sealed JDK internal classes.
@github-actions github-actions Bot added doc-label-missing The Bot applies this label either because none or multiple labels were provided. fixVersion/0.3.0 The feature or bug should be implemented/fixed in the 0.3.0 version. priority/major Default priority of the PR or issue. labels May 14, 2026
Copy link
Copy Markdown
Collaborator

@wenjin272 wenjin272 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @avichaym, Ty for this fix.

The current fix looks good to me, but I think two additional points need to be added:

  1. If user use Map.of to consturct ToolResponseEvent, there will likely be issues as well.
  2. Supplement with relevant tests. Since in the CI pipeline, the test cases in flink-agents-end-to-end-tests-integration module will be executed in jdk 17 with flink 1.20, 2.0, 2.1 and 2.2. I think we can add a EventKryoSerializationTest in this module like:
@Test
void chatRequestEventWithImmutableListSurvivesKryoRoundTrip() throws Exception {
    ChatRequestEvent event =
            new ChatRequestEvent(
                    "myModel", List.of(new ChatMessage(MessageRole.USER, "hello")));

    ChatRequestEvent roundTripped = roundTrip(event, ChatRequestEvent.class);

    assertEquals("myModel", roundTripped.getModel());
    assertNotNull(roundTripped.getMessages());
    assertEquals(1, roundTripped.getMessages().size());
    assertEquals("hello", roundTripped.getMessages().get(0).getContent());
}
private static <T extends Event> T roundTrip(T event, Class<T> cls) throws Exception {
    KryoSerializer<T> serializer = new KryoSerializer<>(cls, new SerializerConfigImpl());
    DataOutputSerializer out = new DataOutputSerializer(64);
    serializer.serialize(event, out);
    DataInputDeserializer in = new DataInputDeserializer(out.getCopyOfBuffer());
    return serializer.deserialize(in);
}

@wenjin272 wenjin272 added doc-not-needed Your PR changes do not impact docs and removed doc-label-missing The Bot applies this label either because none or multiple labels were provided. labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs fixVersion/0.3.0 The feature or bug should be implemented/fixed in the 0.3.0 version. priority/major Default priority of the PR or issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Kryo serialization fails for Event classes with immutable lists on JDK 17+

2 participants