agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: Responses API fails when DeepSeek omits usage.input_tokens_details.cache_write_tokens

Open vincelwx opened this issue 1 month ago • 2 comments

Summary

With AgentScope Java 2.0.4, calling DeepSeek's Responses API through OpenAIResponsesChatModel fails while parsing usage with:

`cache_write_tokens` is not set

The response omits usage.input_tokens_details.cache_write_tokens, but ResponsesHelper.extractUsage() reads it using the OpenAI Java SDK's required-field accessor. A missing usage breakdown consequently fails the model call, including after text has already been streamed.

Environment

  • AgentScope Java: 2.0.4
  • Module: agentscope-extensions-model-openai-official
  • OpenAI Java SDK: 4.52.0 in the tested dependency graph
  • Java: 21
  • Provider: DeepSeek official Responses API
  • Base URL: https://api.deepseek.com
  • Model: deepseek-flash

Reproduction

  1. Configure the Responses model with a valid DeepSeek API key and the endpoint/model above.
  2. Send a simple prompt with streaming enabled and consume the entire stream.
  3. When the terminal response includes usage without cache_write_tokens, usage parsing fails.

Equivalent minimal invocation (API key supplied through an environment variable):

var model = io.agentscope.extensions.model.openaiofficial.OpenAIResponsesChatModel.builder()
        .apiKey(System.getenv("DEEPSEEK_API_KEY"))
        .baseUrl("https://api.deepseek.com")
        .modelName("deepseek-flash")
        .stream(true)
        .build();

model.stream(
        java.util.List.of(new io.agentscope.core.message.UserMessage("Hi")),
        java.util.List.of(),
        io.agentscope.core.model.GenerateOptions.builder()
                .maxTokens(4096)
                .reasoningEffort("none")
                .build())
    .blockLast(java.time.Duration.ofSeconds(60));

The error was observed in an application connection test against DeepSeek. Independently, a local HTTP fixture test using the actual AgentScope adapter and OpenAI SDK reproduces the failure by removing only cache_write_tokens from an otherwise valid terminal Responses payload. It sends a text delta before response.completed, showing that receiving initial text is not sufficient to establish successful completion. The code above is a reduced illustration of the invocation, not a separately executed live-provider test.

Example affected usage fragment (illustrative fixture values):

{
  "usage": {
    "input_tokens": 12,
    "output_tokens": 4,
    "total_tokens": 16,
    "input_tokens_details": { "cached_tokens": 0 },
    "output_tokens_details": { "reasoning_tokens": 0 }
  }
}

Actual behavior / source location

ResponsesHelper.extractUsage() calls:

long cacheCreationTokens = respUsage.inputTokensDetails().cacheWriteTokens();

The SDK throws when that field is absent. The direct accessor is present both in the released 2.0.4 source JAR and in the current main source inspected for this report:

ResponsesHelper.java on main commit 8b06f210.

Expected behavior

A provider omitting a cache-write usage breakdown should not turn an otherwise successful response into a model execution failure. Please handle missing breakdown fields defensively, preserving all reported counts and using the framework's documented fallback semantics for unreported metrics (PR #3215 describes a default of zero). This fallback should not be presented as a measured zero from the provider.

This should apply to both streaming and non-streaming usage extraction. Actual HTTP/authentication errors, response.failed, incomplete responses, and streams missing the required terminal event must remain distinguishable; catching and suppressing the whole parsing exception would lose this distinction.

References

  • DeepSeek Responses API documentation: documents input_tokens_details.cached_tokens and output_tokens_details.reasoning_tokens, but does not document cache_write_tokens.
  • #157 is related in principle (SDK accessors failing on missing response fields), but concerns Chat Completions and a missing id, not this Responses usage field.
  • #3078 introduced the Responses module.

No API keys or application-specific conversation data are included in this report.

vincelwx avatar Oct 14 '26 14:10 vincelwx

其实未打算适配 responses API 兼容厂商,因为这个模块用了 openai-java-sdk,请求和响应解析都要走它自带的DTO,条框限制比较多兼容可能麻烦些。如果大家需求比较多的话,再考虑是否兼容

jujn avatar Oct 14 '26 15:10 jujn

Hi, I'd like to take this one.

I verified the root cause on current main: ResponsesHelper.extractUsage() (agentscope-extensions-model-openai-official) calls the SDK's required-field accessors inputTokensDetails().cacheWriteTokens() / cachedTokens() and outputTokensDetails().reasoningTokens(). These go through JsonField.getRequired and throw when the provider omits the field — DeepSeek's Responses payload only reports input_tokens/output_tokens/total_tokens with no input_tokens_details sub-fields, so the whole model call fails after the text already streamed.

Planned fix: read those three fields through the optional _cacheWriteTokens() / _cachedTokens() / _reasoningTokens() accessors (JSON-field-aware, missing → 0) so partial usage breakdowns still map to a ChatUsage instead of failing the call. I'll add regression tests with usage fixtures that omit each breakdown field.

I'll proceed with this approach unless there's a different preference.

aMetric avatar Oct 15 '26 06:10 aMetric