~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(web): keep a positional template context when injecting the defaults (#7564)
Lukas5272f5c7606c
diff --git a/changelog.d/7564.bugfix.md b/changelog.d/7564.bugfix.mdnew file mode 100644index 0000000000..d6f6e8fe51--- /dev/null+++ b/changelog.d/7564.bugfix.md@@ -0,0 +1 @@+A page rendered with `TemplateResponse(request, name, context)` in positional form no longer fails with a `TypeError`: the positional context now receives the session, CSRF, flash and version defaults like a keyword one.diff --git a/src/local_deep_research/web/template_config.py b/src/local_deep_research/web/template_config.pyindex fe4989ab6e..e07f562f5b 100644--- a/src/local_deep_research/web/template_config.py+++ b/src/local_deep_research/web/template_config.py@@ -15,6 +15,19 @@ from starlette.requests import Requestfrom starlette.responses import Response+# Starlette's TemplateResponse(request, name, context, status_code, headers,+# media_type, background) parameters, in order.+_TEMPLATE_RESPONSE_ARGS = (+ "request",+ "name",+ "context",+ "status_code",+ "headers",+ "media_type",+ "background",+)++class _LDRTemplates(Jinja2Templates):"""Custom Jinja2Templates that injects CSRF token + flash messagesinto every TemplateResponse, so routes can use templates.TemplateResponse@@ -30,6 +43,18 @@ class _LDRTemplates(Jinja2Templates):# Jinja2Templates.TemplateResponse(request, name, context, ...) OR# Jinja2Templates.TemplateResponse(name, context, request=, ...) (legacy) OR# Jinja2Templates.TemplateResponse(request=, name=, context=, ...)+ # Starlette's own form is folded into keywords first, so the context+ # the defaults go into is the one rendered: read from kwargs only, a+ # positional context was replaced by a fresh {} and then passed a+ # second time as context=, a TypeError.+ if args and isinstance(args[0], Request):+ for key, value in zip(_TEMPLATE_RESPONSE_ARGS, args):+ if key in kwargs:+ raise TypeError(+ f"TemplateResponse() got multiple values for argument {key!r}"+ )+ kwargs[key] = value+ args = args[len(_TEMPLATE_RESPONSE_ARGS) :]context = kwargs.get("context") or {}request = kwargs.get("request") or context.get("request")diff --git a/tests/web/test_template_environment_census.py b/tests/web/test_template_environment_census.pyindex 9cd918dcc8..924aeeb29d 100644--- a/tests/web/test_template_environment_census.py+++ b/tests/web/test_template_environment_census.py@@ -677,19 +677,6 @@ def test_render_template_helper_honours_status_code(# ---------------------------------------------------------------------------- strict=True,- reason=(- "DEFECT (latent, unfixed): _LDRTemplates.TemplateResponse reads the "- "context only from kwargs. Called with Starlette's own positional "- "signature TemplateResponse(request, name, context) it builds a "- "throwaway {} , injects the defaults into it, then also passes it "- "as context= alongside the positional one -> TypeError: got "- "multiple values for argument 'context'. All 38 current call sites "- "use keywords, so this is latent; the next one written positionally "- "500s. Remove this xfail when the normalisation is fixed."- ),-)def test_positional_context_call_still_injects_defaults(app_and_templates, make_request):@@ -702,3 +689,29 @@ def test_positional_context_call_still_injects_defaults({"active_page": "research"},)assert f"/releases/tag/v{__version__}" in response.body.decode("utf-8")+++def test_positional_status_code_is_kept(app_and_templates, make_request):+ """Every positional parameter keeps its meaning, not only the context."""+ _, templates = app_and_templates+ response = templates.TemplateResponse(+ make_request({"username": "positional"}),+ "base.html",+ {"active_page": "research"},+ 404,+ )+ assert response.status_code == 404+++def test_context_given_twice_is_a_type_error(app_and_templates, make_request):+ """Positional and keyword context together is a caller bug, as in Python."""+ _, templates = app_and_templates+ with pytest.raises(+ TypeError, match="multiple values for argument 'context'"+ ):+ templates.TemplateResponse(+ make_request({"username": "positional"}),+ "base.html",+ {"active_page": "research"},+ context={"active_page": "history"},+ )