From 823a205fd2aef502baebaa66610767af0035269e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Garc=C3=ADa=20Iglesias?= Date: Sun, 26 Apr 2026 22:00:14 -0300 Subject: [PATCH] feat: interpolate template variables in custom connection headers Admins can now use {{chat_id}}, {{message_id}}, {{user_id}}, and {{user_name}} as dynamic placeholders in custom header values for OpenAI-compatible connections. Before merging custom headers into the outgoing request, get_headers_and_cookies() performs a simple string-replace pass using values already available in the metadata dict and user object. Header values with no {{...}} tokens pass through unchanged, and unknown tokens are left as-is, so the change is fully backwards compatible. Use-case: providers like Manifest that use a session-scoped header (e.g. x-session-key) to route or isolate conversations can now be configured with {"x-session-key": "{{chat_id}}"} so every request automatically carries the current chat UUID. Co-Authored-By: Claude Sonnet 4.6 --- backend/open_webui/routers/openai.py | 14 +++++++++++++- src/lib/components/AddConnectionModal.svelte | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index 6f8c0f81bf..ab9c30cfe9 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -215,7 +215,19 @@ async def get_headers_and_cookies( headers['Authorization'] = f'Bearer {token}' if config.get('headers') and isinstance(config.get('headers'), dict): - headers = {**headers, **config.get('headers')} + template_vars = { + '{{chat_id}}': (metadata or {}).get('chat_id', '') or '', + '{{message_id}}': (metadata or {}).get('message_id', '') or '', + '{{user_id}}': (user.id if user else '') or '', + '{{user_name}}': (user.name if user else '') or '', + } + custom_headers = {} + for k, v in config.get('headers').items(): + if isinstance(v, str): + for token, value in template_vars.items(): + v = v.replace(token, value) + custom_headers[k] = v + headers = {**headers, **custom_headers} return headers, cookies diff --git a/src/lib/components/AddConnectionModal.svelte b/src/lib/components/AddConnectionModal.svelte index ae1d353642..e176ba48af 100644 --- a/src/lib/components/AddConnectionModal.svelte +++ b/src/lib/components/AddConnectionModal.svelte @@ -444,7 +444,7 @@