Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(inference): add OpenAI request timeout config (#2876)

Weilu Jiac0ac8a474fa1
diff --git a/docs/docs/03-configuration/01-environment-variables.md b/docs/docs/03-configuration/01-environment-variables.md
index bb1cfa7c..5d143594 100644
--- a/docs/docs/03-configuration/01-environment-variables.md
+++ b/docs/docs/03-configuration/01-environment-variables.md
@@ -96,6 +96,7 @@ Either `OPENAI_API_KEY` or `OLLAMA_BASE_URL` need to be set for automatic taggin
| OPENAI_API_KEY | No | Not set | The OpenAI key used for automatic tagging. More on that in [here](../06-administration/03-openai.md). |
| OPENAI_BASE_URL | No | Not set | If you just want to use OpenAI you don't need to pass this variable. If, however, you want to use some other openai compatible API (e.g. azure openai service), set this to the url of the API. |
| OPENAI_PROXY_URL | No | Not set | HTTP proxy server URL for OpenAI API requests (e.g., `http://proxy.example.com:8080`). |
+| OPENAI_TIMEOUT_SEC | No | Not set | Timeout for OpenAI API requests in seconds. If unset, the OpenAI SDK default of 10 minutes is used. Increase this alongside `INFERENCE_JOB_TIMEOUT_SEC` when slow inference providers need more time. |
| OPENAI_SERVICE_TIER | No | Not set | Set to `auto`, `default`, or `flex`. Flex processing provides lower costs in exchange for slower response times and occasional resource unavailability. See [OpenAI Flex Processing](https://platform.openai.com/docs/guides/flex-processing) and [Chat Service Tier](https://platform.openai.com/docs/api-reference/chat/object#chat-object-service_tier) for more details. |
| OPENAI_REASONING_EFFORT | No | Not set | Set to `none`, `minimal`, `low`, `medium`, `high` or `xhigh`. Controls the reasoning effort in reasoning models. See [OpenAI: How Reasoning Works](https://developers.openai.com/api/docs/guides/reasoning#how-reasoning-works). |
| OLLAMA_BASE_URL | No | Not set | If you want to use ollama for local inference, set the address of ollama API here. |
diff --git a/packages/shared/config.ts b/packages/shared/config.ts
index 1f81ed11..2a2b96ba 100644
--- a/packages/shared/config.ts
+++ b/packages/shared/config.ts
@@ -60,6 +60,7 @@ const allEnv = z.object({
OPENAI_API_KEY: z.string().optional(),
OPENAI_BASE_URL: z.string().url().optional(),
OPENAI_PROXY_URL: z.string().url().optional(),
+ OPENAI_TIMEOUT_SEC: z.coerce.number().positive().optional(),
OPENAI_SERVICE_TIER: z.enum(["auto", "default", "flex"]).optional(),
OPENAI_REASONING_EFFORT: z
.enum(["none", "minimal", "low", "medium", "high", "xhigh"])
@@ -309,6 +310,7 @@ const serverConfigSchema = allEnv.transform((val, ctx) => {
openAIApiKey: val.OPENAI_API_KEY,
openAIBaseUrl: val.OPENAI_BASE_URL,
openAIProxyUrl: val.OPENAI_PROXY_URL,
+ openAITimeoutSec: val.OPENAI_TIMEOUT_SEC,
openAIServiceTier: val.OPENAI_SERVICE_TIER,
openAIReasoningEffort: val.OPENAI_REASONING_EFFORT,
ollamaBaseUrl: val.OLLAMA_BASE_URL,
diff --git a/packages/shared/inference.ts b/packages/shared/inference.ts
index d58a49af..a7c7e9c8 100644
--- a/packages/shared/inference.ts
+++ b/packages/shared/inference.ts
@@ -140,6 +140,7 @@ export interface OpenAIInferenceConfig {
apiKey: string;
baseURL?: string;
proxyUrl?: string;
+ timeoutSec?: number;
serviceTier?: typeof serverConfig.inference.openAIServiceTier;
textModel: string;
imageModel: string;
@@ -173,6 +174,8 @@ export class OpenAIInferenceClient implements InferenceClient {
this.openAI = new OpenAI({
apiKey: config.apiKey,
baseURL: config.baseURL,
+ timeout:
+ config.timeoutSec !== undefined ? config.timeoutSec * 1000 : undefined,
defaultHeaders: {
"X-Title": "Karakeep",
"HTTP-Referer": "https://karakeep.app",
@@ -188,6 +191,7 @@ export class OpenAIInferenceClient implements InferenceClient {
apiKey: serverConfig.inference.openAIApiKey!,
baseURL: serverConfig.inference.openAIBaseUrl,
proxyUrl: serverConfig.inference.openAIProxyUrl,
+ timeoutSec: serverConfig.inference.openAITimeoutSec,
serviceTier: serverConfig.inference.openAIServiceTier,
textModel: serverConfig.inference.textModel,
imageModel: serverConfig.inference.imageModel,