Skip to content

Studio: canonicalize and validate web_search arguments - #9716

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
Souravrajvi0:feat/web-search-empty-query-ee2d
Oct 2, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
Souravrajvi0:feat/web-search-empty-query-ee2d

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Local models call web_search with argument objects the tool cannot use, and on main those calls are quietly wasted:

  • {"q": "unsloth"} (also search_query, search, text) returns No query provided. without ever searching. OpenAI's hosted agent web tool takes search_query / q, so OpenAI-trained models have seen that shape.
  • {"href": "https://example.com"} (also uri, link) never fetches the page. It does not prompt in "Approve for me" either, because _web_search_fetches_url only reads url.
  • {"query": 123} reaches .strip() in _web_search and raises, which the loop reports back as Error: tool raised an exception: 'int' object has no attribute 'strip'.

What this PR does:

  • Heals the query aliases (q, search_query, search, text) and the URL aliases (uri, href, link) to canonical query / url, and ignores non-string values instead of passing them down.
  • Canonicalizes in ToolLoopController.prepare_call, so the status text, the auto-mode approval gate, the duplicate key and all three tool loops read the same arguments. {"href": "https://example.com"} now prompts in "Approve for me" and then fetches, where before it did neither.
  • Drops query and image_queries from the duplicate key in URL mode, since _web_search returns from the URL branch before reading either, so those calls really are the same fetch.
  • Returns before the search backend when no usable argument is present.

What this does not change

The tool description and schema the model sees are byte-identical to main. An earlier revision added sentences naming the usable arguments; a 5-model A/B showed that was not a safe trade, so it was dropped:

Model correct calls, main description with the added sentences
Qwen3-4B-Instruct-2507 66/105 77/105
Qwen3-8B (no thinking) 105/105 105/105
gemma-4-E4B-it 105/105 105/105
gpt-oss-20b 102/105 103/105
Llama-3.2-3B-Instruct 88/105 75/105 (URL fetches 30/30 -> 19/30)

Greedy decoding, 50 search, 30 link-reading, 20 no-tool and 5 image prompts, with and without image_queries offered; sampled runs (4 per prompt, temperature 0.7) agree. Empty calls were already rare on main (at most 1 in 105 greedy calls per model).

web_search with no usable arguments still returns exactly No query provided., which TOOL_ERROR_PREFIXES already classifies as a recoverable tool error. That is why this PR does not close #9709: for the {} call reported there, main and this branch give the same result string, is_tool_error, duplicate key and model-visible message.

Test plan

From studio/backend:

pytest -q tests/test_web_access_policy.py tests/test_search_images.py \
  tests/test_tool_loop_controller.py tests/test_safetensors_tool_loop.py \
  tests/test_llama_cpp_tool_loop.py tests/test_studio_tool_loop.py tests/test_permission_mode.py

3075 passed. The new alias, URL-alias, non-string and duplicate-key tests fail on main and pass here; every test file that mentions web_search (86 files) has the same failure set on main and on this branch.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

mahiatlinux

This comment was marked as outdated.

@mahiatlinux mahiatlinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed on 6b5c4b0. Malformed values no longer execute, URL-mode duplicates canonicalize correctly, and all three loop callers now assert canonical arguments.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

@mahiatlinux mahiatlinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image-only mode is now documented and tested on 7f6df70.

@oobabooga oobabooga changed the title Studio: recover empty web_search tool calls Studio: canonicalize and validate web_search arguments Aug 26, 2026
@oobabooga

Copy link
Copy Markdown
Member

@Souravrajvi0 I pushed two test commits here and rewrote the title and description. Revert any of it if you disagree.

0bc4141 clears the red (Python 3.13) check. 7f6df70 reworded the tool description to cover image-only mode, and test_web_access_policy.py still asserted the old sentence, so all five parametrizations of test_web_search_empty_arguments_are_a_recoverable_error failed. The schema assertion is now a separate test that checks the fragments instead of one full sentence.

44fc8cc makes test_web_search_rejects_non_string_arguments a real guard. Its four values were all falsy, and not query already short-circuited those on main, so every case passed with or without this PR. 123 and {"a": 1} are the ones that reached .strip() and raised out of _web_search.

The description rewrite drops Fixes #9709. For the {} call that issue reports, this branch and the merge base produce the same result string, the same is_tool_error, the same duplicate key and the same model-visible message, so merging this would close the issue without changing what it describes. The alias healing and the canonicalization in prepare_call are the real content, and the description now says so. #9709 stays open.

@oobabooga

Copy link
Copy Markdown
Member

This PR is in good state, but it seems to solve an unobserved problem.

#9709 concerns {} causing a stalled search; it mentions alias healing only as an optional suggestion, but does not show a model naturally emitting q, search_query, href, or another alias.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the alias and non-string handling in studio/backend/core/inference/tools.py, and that prepare_call now canonicalizes before the duplicate key so {"q": ...} and {"query": ...} collapse together. Do you have a sample from a local model emitting q or href in practice, so I can see how often this comes up before reviewing?

Rebase onto current main. Heal q/search_query/href-style args local
tool parsers emit before execute_tool, and canonicalize in prepare_call.
@Souravrajvi0
Souravrajvi0 force-pushed the feat/web-search-empty-query-ee2d branch from 44fc8cc to a4ee8a6 Compare September 14, 2026 08:23
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen — yes: we do not need a separate “frequency” study; the repo already encodes how often q shows up for local tool streams, and href is the same class of alias miss.

q is not hypothetical — parsers emit it today

On main, the safetensors unrestricted-mode test simulates exactly what a local GGUF stream produces and what reaches execute_tool:

  • Model text: web_search[ARGS]{"q":"x"}
  • execute_tool is invoked with {"q": "x"} (not query)

See studio/backend/tests/test_safetensors_tool_loop.py — test_unrestricted_mode_split_rehearsal_name_is_not_streamed asserts:

exec_fn.calls == [("web_search", {"q": "x"})]

The same {"q": ...} shape appears across the tool-loop/parser suite (test_tool_xml_strip.py, test_tool_call_parser_strict.py, Qwen-style <tool_call>{"name":"web_search","arguments":{"q":"x"}}</tool_call>, etc.). Those tests are the regression harness for local formats, not cloud APIs that always send query.

What breaks on main: execute_tool only reads arguments.get("query") and URL fetch gating only looks at url (tools.py ~9902, _web_search_fetches_url ~4251). So a parsed call with only q becomes No query provided. even though the model clearly intended a search.

With this PR: the same {"q": "unsloth"} runs the search path; prepare_call canonicalizes so status/duplicate keys match.

Quick check on this branch (no DuckDuckGo needed for URL mode):

Arguments main behavior This PR
{"q": "unsloth"} No query provided. search runs
{"href": "https://example.com"} No query provided. (no approval / no fetch) fetches page

href / uri / link

We do not have a dedicated parser fixture that streams href instead of url (URL fetches are less common in the [ARGS]{...} templates). The PR adds test_web_search_heals_url_aliases for uri / href / link because HTML/API docs use those names and small models often mirror them — same motivation as q vs DuckDuckGo’s q parameter.

Scope vs #9709

Agreed this does not change the {} stall (#9709). This only fixes wrong-key but non-empty calls that parsers already deliver as q / href.

Branch

Rebased onto current main today (a4ee8a640) — should be mergeable again; focused 7-file diff unchanged.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from oobabooga Oct 1, 2026
@unslothai unslothai deleted a comment from Souravrajvi0 Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from mahiatlinux Oct 1, 2026
@unslothai unslothai deleted a comment from oobabooga Oct 1, 2026
@unslothai unslothai deleted a comment from oobabooga Oct 1, 2026
@unslothai unslothai deleted a comment from oobabooga Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 1, 2026
A 5-model A/B (greedy + sampled, 105 prompts per arm) showed the added sentences tie on
Qwen3-8B, gemma-4-E4B and gpt-oss-20b, help Qwen3-4B, and regress Llama-3.2-3B URL fetches
(30/30 -> 19/30 greedy), while empty calls were already ~1%. Argument healing stays in code.
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #9716, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#323.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 8e7de2e0f (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/tools.py (on the mirror)

@@ -12630,6 +12630,41 @@ def _edit_file(
 }
 
 
+# Local models often emit q/search_query instead of query, or uri/href instead of url.
+_WEB_SEARCH_QUERY_ALIASES = ("query", "q", "search_query", "search", "text")
+_WEB_SEARCH_URL_ALIASES = ("url", "uri", "href", "link")
+
+
+def _first_nonempty_arg(arguments: dict, keys: tuple[str, ...]) -> str:
+    for key in keys:
+        value = arguments.get(key)
+        if isinstance(value, str) and value.strip():
+            return value.strip()
+    return ""
+
+
+def _resolve_web_search_args(arguments) -> tuple[str, str]:
+    args = arguments if isinstance(arguments, dict) else {}
+    return (
+        _first_nonempty_arg(args, _WEB_SEARCH_QUERY_ALIASES),
+        _first_nonempty_arg(args, _WEB_SEARCH_URL_ALIASES),
+    )
+
+
+def canonicalize_web_search_arguments(arguments) -> dict:
+    # URL mode returns before _web_search reads query or image_queries, so they are dropped from the key.
+    args = dict(arguments) if isinstance(arguments, dict) else {}
+    query, url = _resolve_web_search_args(args)

P2 Badge Preserve the unparsed-arguments sentinel

When a web_search call's JSON is truncated (for example, {"query":"weather in S), coerce_tool_arguments deliberately returns UNPARSED_ARGUMENTS_KEY so execute_tool can report that the arguments were cut off and ask the model to resend them. This canonicalizer discards that sentinel and returns {}, so the call instead executes with a misleading No query provided. result and loses the existing truncation-recovery guidance. Preserve the sentinel rather than filtering it as an unknown web-search argument.

Useful? React with 👍 / 👎.

Accepted on the mirror

Reply from danielhanchen:

Fixed in a5f71c4: prepare_call no longer canonicalizes web_search arguments that carry the unparsed-arguments sentinel, so a truncated call still gets the cut-off error; regression test added.

Round 2: reviewed mirror 77ebc2db44 (this PR's a5f71c4b6)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 77ebc2db44

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 77ebc2db44 (this PR's a5f71c4b6).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit 57ed76c into unslothai:main Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Studio 0.1.803-beta] web_search can be called with empty arguments and returns "No query provided"

4 participants