Skip to content

Fix AnthropicChatOptions.timeout(Duration) being ignored outside model construction#6605

Open
seeun0210 wants to merge 1 commit into
spring-projects:mainfrom
seeun0210:fix/anthropic-per-request-timeout-ignored
Open

Fix AnthropicChatOptions.timeout(Duration) being ignored outside model construction#6605
seeun0210 wants to merge 1 commit into
spring-projects:mainfrom
seeun0210:fix/anthropic-per-request-timeout-ignored

Conversation

@seeun0210

@seeun0210 seeun0210 commented Jul 13, 2026

Copy link
Copy Markdown

Thank you for taking time to contribute this pull request!
You might have already read the contributor guide, but as a reminder, please make sure to:

  • Add a Signed-off-by line to each commit (git commit -s) per the DCO
  • Rebase your changes on the latest main branch and squash your commits
  • Add/Update unit tests as needed
  • Run a build and make sure all tests pass prior to submission

For more details, please check the contributor guide.
Thank you upfront!


Summary

Fixes #6604.

AnthropicChatModel.internalCall()/internalStream() never forwarded the effective AnthropicChatOptions.getTimeout() (whether set via ChatClient.Builder.defaultOptions() or a per-request ChatClient.prompt().options(...) override) into a com.anthropic.core.RequestOptions for the underlying Anthropic Java SDK call. Both call sites always used the SDK's default RequestOptions, so the only timeout that ever had any effect was the one baked into the shared AnthropicChatModel/AnthropicClient once, at construction time (via spring.ai.anthropic.timeout). This contradicts the documented usage, which shows .timeout(Duration) as a per-call AnthropicChatOptions setting.

  • Adds a requestOptionsFor(Prompt) helper that resolves the merged (default + per-request) AnthropicChatOptions.getTimeout() into an explicit RequestOptions, passed to both messageService.create(params, requestOptions) and createStreaming(params, requestOptions).
  • Updates AnthropicChatModelTests' Mockito stubs to match the new two-arg SDK call signature (mechanical change, no behavior change to the tests themselves).
  • Adds AnthropicChatModelTimeoutTests with 3 cases: construction-time timeout bounds a call; a per-request ChatClient timeout overrides a too-short construction-time default; a ChatClient default-options timeout overrides a too-short construction-time default. All 3 fail without the fix (verified) and pass with it.

Test plan

  • ./mvnw -pl models/spring-ai-anthropic test — full module suite (123 tests) passes
  • New AnthropicChatModelTimeoutTests fail without the fix (confirmed via git stash on the production change) and pass with it
  • Verified no real Anthropic API calls are needed — regression tests use MockWebServer with setHeadersDelay(...) for deterministic timing

…l construction

AnthropicChatModel.internalCall()/internalStream() never forwarded the
effective (default or per-request) AnthropicChatOptions.getTimeout()
into a com.anthropic.core.RequestOptions for the underlying SDK call,
so only the timeout baked in once at AnthropicChatModel construction
(via spring.ai.anthropic.timeout) ever had any effect — contradicting
the documented per-call usage.

Add a requestOptionsFor(Prompt) helper that resolves the merged
AnthropicChatOptions timeout into an explicit RequestOptions and pass
it to both the sync create() and async createStreaming() calls.

Fixes spring-projects#6604

Signed-off-by: seeun0210 <cocobell3@naver.com>
@seeun0210 seeun0210 force-pushed the fix/anthropic-per-request-timeout-ignored branch from 2e063b9 to e9f8848 Compare July 13, 2026 02:38
@seeun0210

Copy link
Copy Markdown
Author

While working on this, I noticed the same pattern likely exists in other provider modules — e.g. OpenAiChatModel.internalCall()/internalStream() also call this.openAiClient.chat().completions().create(request) / .createStreaming(request) with no com.openai.core.RequestOptions argument, even though the OpenAI Java SDK exposes the same create(params, RequestOptions) two-arg overload that this fix uses for Anthropic. So OpenAiChatOptions.getTimeout() set via ChatClient (default or per-request) is probably silently ignored there too, for the same reason.

If this fix direction looks right to maintainers, I'm happy to apply the same pattern to OpenAI (and check other provider modules for the same issue) in follow-up PRs.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicChatOptions.timeout(Duration) set via ChatClient (default or per-request) is silently ignored — only construction-time timeout applies

2 participants