[PR #5637] [CLOSED] feat: configurable socket-idle timeouts for AI providers #5506

Closed
opened 2026-06-05 15:21:34 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/5637
Author: @angelplusultra
Created: 5/15/2026
Status: Closed

Base: masterHead: 5631-feat-custom-api-request-timeouts-for-ai-providers


📝 Commits (6)

  • 2205117 add getCustomFetchWithTimeout helper function and apply to all llm providers, agent providers and embedding providers
  • 02a4892 revise .env.example comments
  • c7f32b8 add anthropic
  • 6e9928b fix default ollama socket timeout value
  • 8afad0f hoist agent instantiation out of custom fetch function
  • b24c8fa Merge branch 'master' into 5631-feat-custom-api-request-timeouts-for-ai-providers

📊 Changes

81 files changed (+1022 additions, -60 deletions)

View changed files

📝 server/.env.example (+33 -1)
server/__tests__/utils/AiProviders/helpers/index.test.js (+185 -0)
📝 server/utils/AiProviders/anthropic/index.js (+9 -0)
📝 server/utils/AiProviders/apipie/index.js (+9 -0)
📝 server/utils/AiProviders/azureOpenAi/index.js (+9 -0)
📝 server/utils/AiProviders/cometapi/index.js (+9 -0)
📝 server/utils/AiProviders/deepseek/index.js (+9 -0)
📝 server/utils/AiProviders/dellProAiStudio/index.js (+9 -0)
📝 server/utils/AiProviders/dockerModelRunner/index.js (+5 -0)
📝 server/utils/AiProviders/fireworksAi/index.js (+9 -0)
📝 server/utils/AiProviders/foundry/index.js (+10 -5)
📝 server/utils/AiProviders/gemini/index.js (+9 -0)
📝 server/utils/AiProviders/genericOpenAi/index.js (+9 -0)
📝 server/utils/AiProviders/giteeai/index.js (+9 -0)
📝 server/utils/AiProviders/groq/index.js (+9 -0)
server/utils/AiProviders/helpers/index.js (+86 -0)
📝 server/utils/AiProviders/huggingface/index.js (+9 -0)
📝 server/utils/AiProviders/koboldCPP/index.js (+9 -0)
📝 server/utils/AiProviders/lemonade/index.js (+11 -0)
📝 server/utils/AiProviders/liteLLM/index.js (+9 -0)

...and 61 more files

📄 Description

Pull Request Type

  • feat (New feature)
  • 🐛 fix (Bug fix)
  • ♻️ refactor (Code refactoring without changing behavior)
  • 💄 style (UI style changes)
  • 🔨 chore (Build, CI, maintenance)
  • 📝 docs (Documentation updates)

Relevant Issues

resolves #5631
resolves #5603
resolves #5681

Description

Adds a per-provider, env-configurable socket-idle timeout for every AI provider whose SDK exposes a fetch injection point (the OpenAI SDK, Ollama's client, and the Anthropic SDK), so slow local models stop tripping the SDK's hardcoded 5-minute socket idle timeout and surfacing ERR_SOCKET_TIMEOUT.

New helper

server/utils/AiProviders/helpers/index.js — getFetchWithCustomTimeout(rawTimeoutValue, providerSlog, fallbackTimeoutValue?). Given an env value, returns either the global fetch (when no timeout is resolved) or a wrapped fetch whose dispatcher is an undici.Agent with headersTimeout and bodyTimeout set to the resolved value. This bypasses the OpenAI SDK's node-fetch + agentkeepalive stack — that's where the 5-minute idle cap lives — and applies to gaps between streamed chunks as well as the wait for response headers.

Resolution order: valid positive integer → use it; invalid value → log via the provider's slog and fall through; fallback (when provided) → use it; otherwise → unwrapped fetch.

Note on retries: the OpenAI SDK retries failed requests (default maxRetries: 2), so a timeout-aborted attempt can still be retried up to twice before surfacing. Not changed here, but documented in the helper JSDoc for callers who want a hard wall-clock cap.

Applied to

The helper is wired into every provider that constructs an OpenAI SDK client, an Ollama client, or an Anthropic SDK client across three layers:

  • server/utils/AiProviders/* — LLM providers
  • server/utils/EmbeddingEngines/* — embedding engines
  • server/utils/agents/aibitat/providers/* — agent providers

Each constructor now passes fetch: getFetchWithCustomTimeout(process.env._RESPONSE_TIMEOUT, .slog). Several #slog private static loggers were promoted to public slog so the helper could surface its setup/parse logs through the right provider prefix.

Ollama refactor

Ollama previously had its own applyOllamaFetch() that only honored timeouts greater than 5 minutes. That bespoke implementation is removed in favor of the shared helper, and a 15-minute fallback (DEFAULT_OLLAMA_SOCKET_TIMEOUT = 900000) is passed in so existing setups keep working without env changes.

Out of scope

Cohere and AWS Bedrock are not covered. Their SDKs do not expose a standard fetch injection point compatible with this helper:

  • Cohere accepts a fetcher option, but its signature is Fern's (args: Fetcher.Args) => Promise, not standard fetch — wiring it would require a Cohere-specific adapter.
  • Bedrock builds on @smithy/node-http-handler over node:http, not fetch — it would need a Smithy requestHandler with requestTimeout instead.

These can be added in follow-ups; they're intentionally deferred to keep this PR focused on the providers the same helper can cover unmodified.

Docs

server/.env.example documents _RESPONSE_TIMEOUT for each affected provider. The comment phrasing makes clear this is a socket-idle timeout (waiting for headers, or a gap between streamed chunks) — not a total request timeout, since a long-running streaming response can legitimately exceed this value.

Visuals (if applicable)

N/A — backend change with no UI surface.

Additional Information

  • Unit tests for the helper live in server/tests/utils/AiProviders/helpers/index.test.js and cover the resolution order: undefined/null/empty/non-numeric/zero/negative values, valid integers, and fallback behavior.
  • The previous Ollama-specific OLLAMA_RESPONSE_TIMEOUT env var keeps working as before; its .env.example entry was normalized to the same phrasing as the others and the stale "default is 5min" note was corrected to 15min to match the new fallback.

Developer Validations

  • I ran yarn lint from the root of the repo and committed changes
  • Relevant documentation has been updated (if applicable)
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Mintplex-Labs/anything-llm/pull/5637 **Author:** [@angelplusultra](https://github.com/angelplusultra) **Created:** 5/15/2026 **Status:** ❌ Closed **Base:** `master` ← **Head:** `5631-feat-custom-api-request-timeouts-for-ai-providers` --- ### 📝 Commits (6) - [`2205117`](https://github.com/Mintplex-Labs/anything-llm/commit/22051174adc62ea93e88c79496b4384a54b5511d) add getCustomFetchWithTimeout helper function and apply to all llm providers, agent providers and embedding providers - [`02a4892`](https://github.com/Mintplex-Labs/anything-llm/commit/02a48926693cd261d2425301587da9ed2eaed8a4) revise .env.example comments - [`c7f32b8`](https://github.com/Mintplex-Labs/anything-llm/commit/c7f32b8a635b7366a738a93d7d9062606fcb039f) add anthropic - [`6e9928b`](https://github.com/Mintplex-Labs/anything-llm/commit/6e9928bc8f289877c91a17cc3db8d889a0ba87e9) fix default ollama socket timeout value - [`8afad0f`](https://github.com/Mintplex-Labs/anything-llm/commit/8afad0f7b05f1e868fd674477b84ba68f7e0172f) hoist agent instantiation out of custom fetch function - [`b24c8fa`](https://github.com/Mintplex-Labs/anything-llm/commit/b24c8fa1e4b6de55826af1dcb9aba1626bee7728) Merge branch 'master' into 5631-feat-custom-api-request-timeouts-for-ai-providers ### 📊 Changes **81 files changed** (+1022 additions, -60 deletions) <details> <summary>View changed files</summary> 📝 `server/.env.example` (+33 -1) ➕ `server/__tests__/utils/AiProviders/helpers/index.test.js` (+185 -0) 📝 `server/utils/AiProviders/anthropic/index.js` (+9 -0) 📝 `server/utils/AiProviders/apipie/index.js` (+9 -0) 📝 `server/utils/AiProviders/azureOpenAi/index.js` (+9 -0) 📝 `server/utils/AiProviders/cometapi/index.js` (+9 -0) 📝 `server/utils/AiProviders/deepseek/index.js` (+9 -0) 📝 `server/utils/AiProviders/dellProAiStudio/index.js` (+9 -0) 📝 `server/utils/AiProviders/dockerModelRunner/index.js` (+5 -0) 📝 `server/utils/AiProviders/fireworksAi/index.js` (+9 -0) 📝 `server/utils/AiProviders/foundry/index.js` (+10 -5) 📝 `server/utils/AiProviders/gemini/index.js` (+9 -0) 📝 `server/utils/AiProviders/genericOpenAi/index.js` (+9 -0) 📝 `server/utils/AiProviders/giteeai/index.js` (+9 -0) 📝 `server/utils/AiProviders/groq/index.js` (+9 -0) ➕ `server/utils/AiProviders/helpers/index.js` (+86 -0) 📝 `server/utils/AiProviders/huggingface/index.js` (+9 -0) 📝 `server/utils/AiProviders/koboldCPP/index.js` (+9 -0) 📝 `server/utils/AiProviders/lemonade/index.js` (+11 -0) 📝 `server/utils/AiProviders/liteLLM/index.js` (+9 -0) _...and 61 more files_ </details> ### 📄 Description ### Pull Request Type - [x] ✨ feat (New feature) - [ ] 🐛 fix (Bug fix) - [ ] ♻️ refactor (Code refactoring without changing behavior) - [ ] 💄 style (UI style changes) - [ ] 🔨 chore (Build, CI, maintenance) - [ ] 📝 docs (Documentation updates) ### Relevant Issues resolves #5631 resolves #5603 resolves #5681 ### Description Adds a per-provider, env-configurable socket-idle timeout for every AI provider whose SDK exposes a fetch injection point (the OpenAI SDK, Ollama's client, and the Anthropic SDK), so slow local models stop tripping the SDK's hardcoded 5-minute socket idle timeout and surfacing ERR_SOCKET_TIMEOUT. **New helper** server/utils/AiProviders/helpers/index.js — getFetchWithCustomTimeout(rawTimeoutValue, providerSlog, fallbackTimeoutValue?). Given an env value, returns either the global fetch (when no timeout is resolved) or a wrapped fetch whose dispatcher is an undici.Agent with headersTimeout and bodyTimeout set to the resolved value. This bypasses the OpenAI SDK's node-fetch + agentkeepalive stack — that's where the 5-minute idle cap lives — and applies to gaps between streamed chunks as well as the wait for response headers. Resolution order: valid positive integer → use it; invalid value → log via the provider's slog and fall through; fallback (when provided) → use it; otherwise → unwrapped fetch. Note on retries: the OpenAI SDK retries failed requests (default maxRetries: 2), so a timeout-aborted attempt can still be retried up to twice before surfacing. Not changed here, but documented in the helper JSDoc for callers who want a hard wall-clock cap. **Applied to** The helper is wired into every provider that constructs an OpenAI SDK client, an Ollama client, or an Anthropic SDK client across three layers: - server/utils/AiProviders/* — LLM providers - server/utils/EmbeddingEngines/* — embedding engines - server/utils/agents/aibitat/providers/* — agent providers Each constructor now passes fetch: getFetchWithCustomTimeout(process.env.<PROVIDER>_RESPONSE_TIMEOUT, <Class>.slog). Several #slog private static loggers were promoted to public slog so the helper could surface its setup/parse logs through the right provider prefix. **Ollama refactor** Ollama previously had its own applyOllamaFetch() that only honored timeouts greater than 5 minutes. That bespoke implementation is removed in favor of the shared helper, and a 15-minute fallback (DEFAULT_OLLAMA_SOCKET_TIMEOUT = 900000) is passed in so existing setups keep working without env changes. **Out of scope** Cohere and AWS Bedrock are not covered. Their SDKs do not expose a standard fetch injection point compatible with this helper: - Cohere accepts a fetcher option, but its signature is Fern's (args: Fetcher.Args) => Promise<APIResponse>, not standard fetch — wiring it would require a Cohere-specific adapter. - Bedrock builds on @smithy/node-http-handler over node:http, not fetch — it would need a Smithy requestHandler with requestTimeout instead. These can be added in follow-ups; they're intentionally deferred to keep this PR focused on the providers the same helper can cover unmodified. **Docs** server/.env.example documents <PROVIDER>_RESPONSE_TIMEOUT for each affected provider. The comment phrasing makes clear this is a socket-idle timeout (waiting for headers, or a gap between streamed chunks) — not a total request timeout, since a long-running streaming response can legitimately exceed this value. ### Visuals (if applicable) N/A — backend change with no UI surface. ### Additional Information - Unit tests for the helper live in server/__tests__/utils/AiProviders/helpers/index.test.js and cover the resolution order: undefined/null/empty/non-numeric/zero/negative values, valid integers, and fallback behavior. - The previous Ollama-specific OLLAMA_RESPONSE_TIMEOUT env var keeps working as before; its .env.example entry was normalized to the same phrasing as the others and the stale "default is 5min" note was corrected to 15min to match the new fallback. ### Developer Validations - [x] I ran yarn lint from the root of the repo and committed changes - [x] Relevant documentation has been updated (if applicable) - [x] I have tested my code functionality - [x] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 15:21:34 -04:00
yindo closed this issue 2026-06-05 15:21:34 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#5506