Skip to content

[Bug]: ThinkingAccumulator drops earlier streamed reasoning_details during replay #3127

Closed

Description

@pengmoubuaixuexi

Describe the bug

On current main, streamed OpenAI-compatible reasoning_details survive parsing individually but earlier chunks are lost when ReasoningContext assembles the final message. ThinkingAccumulator.add() merges metadata with putAll; every chunk uses the same reasoningDetails key, so the last list replaces all previous lists.

This is distinct from #2913/#2914 (thinking/tool metadata overwriting inside the converter) and #2909/#2910 (typed metadata after JSON persistence). This reproduction uses the latest main after those fixes, without tool calls or persistence.

To Reproduce

Add the following test to agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai/src/test/java/io/agentscope/extensions/model/openai/formatter/StreamingReasoningDetailsReplayTest.java:

/*
 * Copyright 2024-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.agentscope.extensions.model.openai.formatter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.agentscope.core.agent.accumulator.ReasoningContext;
import io.agentscope.core.util.JsonUtils;
import io.agentscope.extensions.model.openai.dto.OpenAIMessage;
import io.agentscope.extensions.model.openai.dto.OpenAIReasoningDetail;
import io.agentscope.extensions.model.openai.dto.OpenAIResponse;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;

class StreamingReasoningDetailsReplayTest {

    @Test
    void preservesDistinctReasoningBlocksAcrossStreamingChunks() {
        OpenAIResponseParser parser = new OpenAIResponseParser();
        ReasoningContext context = new ReasoningContext("assistant");
        for (int index = 0; index < 2; index++) {
            String json = """
                    {"id":"reply-1","object":"chat.completion.chunk","choices":[
                      {"index":0,"delta":{"reasoning_details":[
                        {"type":"reasoning.encrypted","id":"rs_%d","index":%d,
                         "format":"anthropic-claude-v1","data":"opaque_%d"}
                      ]}}
                    ]}
                    """.formatted(index, index, index);
            OpenAIResponse response =
                    JsonUtils.getJsonCodec().fromJson(json, OpenAIResponse.class);
            context.processChunk(parser.parseResponse(response, Instant.now()));
        }

        OpenAIMessage replay =
                new OpenAIMessageConverter(m -> "", b -> "")
                        .convertToMessage(context.buildFinalMessage(), false);
        assertNotNull(replay.getReasoningDetails());
        assertEquals(
                List.of("rs_0", "rs_1"),
                replay.getReasoningDetails().stream().map(OpenAIReasoningDetail::getId).toList());
        assertEquals(
                List.of("opaque_0", "opaque_1"),
                replay.getReasoningDetails().stream().map(OpenAIReasoningDetail::getData).toList());
    }
}

From the repository root:

mvn -B -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai -am -Dtest=StreamingReasoningDetailsReplayTest -Dsurefire.failIfNoSpecifiedTests=false -Dspotless.skip=true test

This runs the actual JSON DTO parser -> OpenAIResponseParser -> ReasoningContext -> OpenAIMessageConverter pipeline, using synthetic local chunks and no model API key.

Expected behavior

Both distinct reasoning records, in received order, are present in the follow-up message: [rs_0, rs_1], with their opaque payloads unchanged.

Error messages

expected: <[rs_0, rs_1]> but was: <[rs_1]>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

Environment

  • AgentScope Java: 2.0.3-SNAPSHOT, main 7399ed61b2ea22b25e2ead4dec9d59df6a77785c
  • Java: Temurin 17.0.15
  • Maven: 3.9.12
  • OS: Windows 11

Additional context

The observed impact is loss of earlier reasoning metadata in the replay payload. No live provider rejection is claimed. OpenRouter documents that the full reasoning-details sequence must be preserved for replay: https://openrouter.ai/docs/guides/best-practices/reasoning-tokens#preserving-reasoning

I also checked #2335, #2418 and #2233, which touch the same accumulator for native Gemini/Anthropic signatures. Their current patches do not accumulate the OpenAI reasoningDetails lists. The proposed focused fix is to accumulate this list-valued metadata key in stream order, while preserving existing last-value behavior for other metadata and clearing the list on reset. The test should also cover snapshots and reset to prevent metadata leaking between rounds.

I intend to submit a small linked fix with regression tests. AI assistance was used for source review and reproduction; the failure above was executed locally against the stated commit.

Activity

  1. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed

    Verified against current main (7399ed6) by source-level analysis:

    • Root cause: ThinkingAccumulator.add(ThinkingBlock) merges chunk metadata with metadata.putAll(block.getMetadata()) (agentscope-core/src/main/java/io/agentscope/core/agent/accumulator/ThinkingAccumulator.java:39-46). Every streamed chunk writes its parsed list under the same key ThinkingBlock.METADATA_REASONING_DETAILS (set in OpenAIResponseParser.java:440-445), so each putAll replaces the previous list instead of appending to it. ReasoningContext feeds each chunk into that accumulator and later emits the aggregated block, so only the last chunk's reasoning_details reach the built message — matching the reported expected <[rs_0, rs_1]> but was <[rs_1]>.
    • Impact: streaming OpenAI-compatible providers that spread reasoning_details across chunks; earlier encrypted signatures / reasoning entries are lost in the replay payload that OpenAIMessageConverter later reads.
    • Severity: high.
    • Suggested direction: special-case the list-valued reasoningDetails key in ThinkingAccumulator (append in stream order, clear on reset) and keep last-value semantics for other metadata keys; add a regression test covering multi-chunk accumulation, snapshots, and reset isolation.

    Note: no reproduction/build was run here (no JDK/Maven in this environment); conclusion is source-based. The reporter already opened PR #3128 with a focused fix plus regression tests, so no separate fix branch will be generated for this issue — review will happen on that PR.


    Automated evaluation by github-manager-bot

  2. added
    area/core/agent
    bug
    on Sep 13, 2026
  3. added a commit that references this issue on Sep 13, 2026
    39cd304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions