Skip to content

Allow closing OpenAiChatModel-owned OpenAI clients - #7159

Open
Emryswu wants to merge 2 commits into
spring-projects:mainfrom
Emryswu:openai-chat-model-close
Open

Emryswu wants to merge 2 commits into
spring-projects:mainfrom
Emryswu:openai-chat-model-close

Conversation

@Emryswu

@Emryswu Emryswu commented Oct 14, 2026

Copy link
Copy Markdown

Fixes #7155

OpenAiChatModel now implements AutoCloseable. close() only closes the OpenAI clients that the builder created itself; clients supplied via openAiClient(...) / openAiClientAsync(...) are left to the caller, so the existing client-sharing path keeps working.

  • Ownership is tracked per client, so mixed construction (e.g. only the sync client supplied) closes just the internally created one
  • Both owned clients are closed even if one fails; a second failure is added as suppressed
  • Repeated close() calls are a no-op, including after a failure

Tests cover borrowed, owned, mixed, failure and double-close cases.

This PR is intentionally scoped to OpenAiChatModel. The other SDK-based OpenAI models and the auto-configuration follow the same pattern; happy to extend the change to them here or in a follow-up, depending on what you prefer.

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.

No way to close OpenAiChatModel — rebuilding it at runtime leaks openai-java client threads (DefaultSleeper)

2 participants