hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

OpenRouter: z-ai model namespace disables required tool choice for endpoints that support it

Open agolanov opened this issue 1 month ago • 0 comments

Bug Description

Hindsight infers that every endpoint serving a z-ai/... model rejects non-auto tool choice. This removes tool_choice="required" even when OpenRouter routes the request to an endpoint that supports it.

This appears to be an overgeneralization of #4246 / #4438. That report reproduced HTTP 400 with OpenRouter explicitly pinned to provider.only: ["z-ai"]. The model namespace identifies the model vendor, but does not identify the endpoint selected by OpenRouter: the same model is also served by Wafer, DeepInfra, DigitalOcean, etc.

The relevant logic is still present in _rejects_non_auto_tool_choice on main: after checking the provider, it treats any z-ai / zai namespace as proof that non-auto tool choice is unsupported.

Steps to Reproduce

  1. Configure Hindsight with an OpenRouter-backed GLM model and pin a compatible endpoint:

    HINDSIGHT_API_LLM_PROVIDER=openrouter
    HINDSIGHT_API_LLM_MODEL=z-ai/glm-5.3-flash
    HINDSIGHT_API_LLM_EXTRA_BODY='{"provider":{"only":["wafer"],"require_parameters":true,"allow_fallbacks":false}}'
    
  2. Populate a bank with a known fact and run Reflect with a question requiring that fact. Inspect the outgoing tool-call request where Reflect requests a named/required retrieval or completion tool. The adapter removes the required tool-choice field because of the model prefix, before the selected endpoint has rejected anything.

  3. Compare the same tool request sent directly to the pinned endpoint through OpenRouter, with and without tool_choice="required". In our live tests, the Wafer route accepted required and returned the tool call; omission allowed prose responses.

The classification error is also reproducible without API calls. Against an upstream source checkout:

import ast
from pathlib import Path
from types import SimpleNamespace

path = Path("hindsight-api-slim/hindsight_api/engine/providers/openai_compatible_llm.py")
tree = ast.parse(path.read_text())
names = {
    "_NON_AUTO_TOOL_CHOICE_UNSUPPORTED_PROVIDERS",
    "_NON_AUTO_TOOL_CHOICE_UNSUPPORTED_MODEL_VENDORS",
    "_rejects_non_auto_tool_choice",
}
nodes = [
    n for n in ast.walk(tree)
    if (isinstance(n, ast.FunctionDef) and n.name in names)
    or (isinstance(n, ast.Assign) and any(
        isinstance(t, ast.Name) and t.id in names for t in n.targets
    ))
]
scope = {}
exec(compile(ast.Module(body=nodes, type_ignores=[]), str(path), "exec"), scope)
endpoint = SimpleNamespace(
    provider="openrouter", model="z-ai/glm-5.3-flash", base_url=None,
)
print(scope["_rejects_non_auto_tool_choice"](endpoint))  # True, regardless of route

This last check demonstrates the classification, not endpoint support; the latter was tested separately with live requests.

Expected Behavior

Preserve the caller's required/named tool-choice contract for OpenRouter endpoints that support it. A model-vendor prefix alone should not disable it.

Keep the compatibility handling for the actual Z.AI endpoint described in #4246, including when that endpoint is reached through OpenRouter. Endpoint-aware handling, or a fallback based on the actual unsupported-parameter response, would avoid treating all hosts of the model alike.

Actual Behavior

Required tool choice is silently removed for all z-ai/... routes. The model can return prose instead of the requested tool, leading to extra Reflect turns and, in our setup, synthesis timeouts / fallback retrieval.

Measurements from our investigation:

  • A small direct Wafer probe returned the requested tool 3/3 times with explicit tool choice. Under auto/omitted choice, 2/3 responses were prose. This is a small sample, not a reliability estimate.
  • Locally preserving required tool choice for OpenRouter, together with restricting routing to verified endpoints and adjusting operation timeouts, produced 10/10 successful coding-agent synthesis hooks (five Codex, five Claude; approximately 11–20 seconds). Their traces contained 40 required/named tool requests and 40 tool-call completions.
  • The end-to-end result includes configuration changes, so it is not an isolated benchmark of the code patch. The classification reproducer and direct endpoint comparison are the narrower evidence for this bug.

Our local patch exempts OpenRouter from the namespace heuristic. It is a workaround for our selected routes, not a proposed universal fix: simply exempting all OpenRouter traffic could regress the Z.AI route in #4246.

We also encountered slow or noncompliant upstream endpoints. Those are separate provider issues; this report concerns Hindsight removing a supported parameter before making the request.

Version

Hindsight 1.1.0, self-hosted Docker on Linux/WSL2. The same namespace heuristic is present on upstream main at 0691b504a88480844b2db53384b8c81971b7cf1e.

LLM Provider

OpenRouter, model z-ai/glm-5.3-flash; Wafer was used for the direct tool-choice comparison.

agolanov avatar Oct 23 '26 00:10 agolanov