[PR #701] [CLOSED] serve: a malformed "tools" is a 400 naming the field (#592) #2774

Closed
opened 2026-11-04 02:03:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/701
Author: @bsorensen110
Created: 10/4/2026
Status: ❌ Closed

Base: main ← Head: fix/serve-tools-400


📝 Commits (3)

  • 0b5aa7a serve: a malformed "tools" is a 400 naming the field (#592)
  • 883b0bb Fix: handle wrapped tool calls also
  • 1d9a8d0 serve/test_server.py: the wrapped-tool case the Copilot review found

📊 Changes

3 files changed (+116 additions, -5 deletions)

View changed files

📝 serve/frontend.py (+33 -4)
📝 serve/server.py (+1 -1)
📝 serve/test_server.py (+82 -0)

📄 Description

Summary

A request whose tools is not a list of tool objects takes the request thread down with an AttributeError/TypeError/KeyError. The client sees a connection reset (a reverse proxy shows 502), and it looks like the engine died. This validates tools where the request is parsed, so the caller's existing ValueError → 400 path answers with a message naming the field. Fixes #592.

Where it broke

tools was the one request field that skipped the repo's shape validation:

  • serve/frontend.py (OpenAI path): t.get("function", t) if isinstance(t, dict) and ... else t — a non-dict entry is handed on unchanged;
  • serve/server.py:2422: own = {t.get("name") for t in tools or []} — assumes every entry is a mapping → AttributeError: 'str' object has no attribute 'get';
  • serve/frontend.py (Anthropic path): t["name"] → TypeError: string indices must be integers on a string, KeyError on a dict without name.

The same file already has the right convention for messages and tool_calls (_object_list, #460): decode a double-encoded JSON string, accept None as no field, raise ValueError for anything else — the server turns that into a 400 naming the field. tools just wasn't wired through it.

The change

  • serve/frontend.py: new _tools_of() (the #460 convention) used by both openai_to_messages and anthropic_to_messages; the Anthropic path additionally requires a non-empty "name" string per tool.
  • serve/server.py: the consumer keeps if isinstance(t, dict) as a second line of defence (the reporter's suggestion).
  • serve/test_server.py: ToolsShape, 4 tests on the mock engine — no GPU, no pack.

No behavior change for valid requests: flat-form tools, {"type":"function",...} tools, a double-encoded JSON string, and an absent field all behave exactly as before.

Evidence

RED, reproduced on this repo at 99f3dbd — mock server (python -m serve.server --engine mock --port 8095), the issue's three repros verbatim:

request tools before after
"auto" connection reset, no reply 400 — tools must be a list of objects
["get_weather"] connection reset, no reply 400 — tools must be a list of objects
[{"name":"get_weather"}] (OpenAI) accepted (200) accepted (200) — unchanged
Anthropic [{"name":"get_weather"}] accepted (200) accepted (200) — unchanged
Anthropic [{"description":"no name"}] KeyError → thread dies 400 — tools: every tool needs a "name" string

Server log before the fix: 4 Traceback entries (AttributeError: 'str' object has no attribute 'get' at serve/server.py:2422), matching the report.

Unit tests (python -m unittest serve.test_server.ToolsShape -v):

  • on origin/main: Ran 4 tests ... FAILED (errors=4) — every malformed case raises http.client.RemoteDisconnected: Remote end closed connection without response, the exact reported symptom;
  • with the fix: 4/4 pass.

Regression (mock engine, no GPU): full serve.test_server 122 tests OK; serve.test_security, test_structured, test_monitor, test_lifecycle, test_detok, test_mcp all OK.

Test plan

  • ToolsShape RED on main (4 errors: RemoteDisconnected), GREEN with the fix
  • Full serve.test_server (122) + security/structured/monitor/lifecycle/detok/mcp suites pass
  • Issue's repro commands re-run against a mock server: 400s naming tools, server answers the next request normally
  • CI

🤖 Generated with Hermes Agent


🔄 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/Niko1221/Strata/pull/701 **Author:** [@bsorensen110](https://github.com/bsorensen110) **Created:** 10/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/serve-tools-400` --- ### 📝 Commits (3) - [`0b5aa7a`](https://github.com/Niko1221/Strata/commit/0b5aa7a4c10b6063d11b608ce0cc0a6a75ec8c45) serve: a malformed "tools" is a 400 naming the field (#592) - [`883b0bb`](https://github.com/Niko1221/Strata/commit/883b0bb490b3564ab7217efd576bf3155ef0f1c6) Fix: handle wrapped tool calls also - [`1d9a8d0`](https://github.com/Niko1221/Strata/commit/1d9a8d026ec99873ab72510991dca630dd440156) serve/test_server.py: the wrapped-tool case the Copilot review found ### 📊 Changes **3 files changed** (+116 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+33 -4) 📝 `serve/server.py` (+1 -1) 📝 `serve/test_server.py` (+82 -0) </details> ### 📄 Description ## Summary A request whose `tools` is not a list of tool objects takes the request thread down with an `AttributeError`/`TypeError`/`KeyError`. The client sees a connection reset (a reverse proxy shows `502`), and it looks like the engine died. This validates `tools` where the request is parsed, so the caller's existing `ValueError → 400` path answers with a message naming the field. Fixes #592. ## Where it broke `tools` was the one request field that skipped the repo's shape validation: - `serve/frontend.py` (OpenAI path): `t.get("function", t) if isinstance(t, dict) and ... else t` — a non-dict entry is handed on unchanged; - `serve/server.py:2422`: `own = {t.get("name") for t in tools or []}` — assumes every entry is a mapping → `AttributeError: 'str' object has no attribute 'get'`; - `serve/frontend.py` (Anthropic path): `t["name"]` → `TypeError: string indices must be integers` on a string, `KeyError` on a dict without `name`. The same file already has the right convention for `messages` and `tool_calls` (`_object_list`, #460): decode a double-encoded JSON string, accept `None` as no field, raise `ValueError` for anything else — the server turns that into a 400 naming the field. `tools` just wasn't wired through it. ## The change - `serve/frontend.py`: new `_tools_of()` (the #460 convention) used by both `openai_to_messages` and `anthropic_to_messages`; the Anthropic path additionally requires a non-empty `"name"` string per tool. - `serve/server.py`: the consumer keeps `if isinstance(t, dict)` as a second line of defence (the reporter's suggestion). - `serve/test_server.py`: `ToolsShape`, 4 tests on the mock engine — no GPU, no pack. No behavior change for valid requests: flat-form tools, `{"type":"function",...}` tools, a double-encoded JSON string, and an absent field all behave exactly as before. ## Evidence **RED, reproduced on this repo at `99f3dbd`** — mock server (`python -m serve.server --engine mock --port 8095`), the issue's three repros verbatim: | request `tools` | before | after | |---|---|---| | `"auto"` | connection reset, no reply | `400` — `tools must be a list of objects` | | `["get_weather"]` | connection reset, no reply | `400` — `tools must be a list of objects` | | `[{"name":"get_weather"}]` (OpenAI) | accepted (200) | accepted (200) — unchanged | | Anthropic `[{"name":"get_weather"}]` | accepted (200) | accepted (200) — unchanged | | Anthropic `[{"description":"no name"}]` | `KeyError` → thread dies | `400` — `tools: every tool needs a "name" string` | Server log before the fix: 4 `Traceback` entries (`AttributeError: 'str' object has no attribute 'get'` at `serve/server.py:2422`), matching the report. **Unit tests** (`python -m unittest serve.test_server.ToolsShape -v`): - on `origin/main`: `Ran 4 tests ... FAILED (errors=4)` — every malformed case raises `http.client.RemoteDisconnected: Remote end closed connection without response`, the exact reported symptom; - with the fix: 4/4 pass. **Regression** (mock engine, no GPU): full `serve.test_server` 122 tests OK; `serve.test_security`, `test_structured`, `test_monitor`, `test_lifecycle`, `test_detok`, `test_mcp` all OK. ## Test plan - [x] `ToolsShape` RED on main (4 errors: RemoteDisconnected), GREEN with the fix - [x] Full `serve.test_server` (122) + security/structured/monitor/lifecycle/detok/mcp suites pass - [x] Issue's repro commands re-run against a mock server: 400s naming `tools`, server answers the next request normally - [ ] CI 🤖 Generated with Hermes Agent --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:03:02 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2774
No description provided.