Skip to content

feat(openai): complete hosted-discovery function execution - #9085

Open
Yafe (YAfe27) wants to merge 7 commits into
crewAIInc:mainfrom
YAfe27:feat/hosted-tool-discovery
Open

Yafe (YAfe27) wants to merge 7 commits into
crewAIInc:mainfrom
YAfe27:feat/hosted-tool-discovery

Conversation

@YAfe27

@YAfe27 Yafe (YAfe27) commented Nov 4, 2026 •

Copy link
Copy Markdown

Related issue

Fixes #8125.

Summary

Hosted-search function calls use the existing CrewAI native execution path and the caller's authorized tool catalog. This completes the execution milestone on top of #8040 with behavioral coverage and validation of malformed arguments.

The existing executor already resolves function names and returns results under the matching call_id. The remaining validation gap allowed JSON null or [] to become empty arguments and execute a tool using its defaults. The shared argument parser now rejects non-object JSON before execution and returns the existing invalid-input failure with the original call ID. Valid eager function calls keep their existing behavior.

The new fake Responses sequences cover successful hosted discovery followed by a function call, an unknown function advertised by discovery, malformed JSON and non-object arguments, and tool failure. The same cases run with eager loading. They check the authorized request catalog, execution counts, matching result IDs, and synchronous non-streaming behavior.

Verification

  • Tests added or updated for the changed behavior

  • Relevant tests and quality checks pass locally

  • 286 tests passed across the hosted-execution tests, prerequisite request-building tests, existing Responses tests, agent utilities, native JSON parsing, and tool-failure tests.

  • The six non-object argument cases failed before the parser fix; null and [] executed the tool with default arguments.

  • Ruff and formatting checks passed on our changed source and test file. Strict mypy runs through the repository's commit hook; the new test file also passes a targeted type check.

  • Test responses are local SDK objects with a fake client; no paid API calls or credentials are required.

Additional context

Depends on #8040, which is still open. Its original commits and authorship are included in this branch. The configuration, request builder, and translated documentation belong to that prerequisite; this contribution adds the argument-validation change and test_hosted_tool_execution.py in a separate final commit.

Syed Ahnaf Raza (@syahra712), this uses your existing tool_search / defer_loading interface unchanged. It adds no competing configuration or tool catalog. #7972's separate streaming work is not included.

Prepared with OpenAI Codex assistance. The required llm-generated label needs a maintainer because this account does not have permission to apply repository labels.


Note

Medium Risk
Changes the shared native tool-argument parser and OpenAI Responses request assembly; behavior shifts for non-object tool args and large tool catalogs when tool_search is enabled.

Overview
This PR finishes hosted tool discovery on the OpenAI Responses API path and tightens how native function calls validate arguments.

Responses API tool_search: OpenAICompletion gains opt-in tool_search (normalized from True/False to OpenAIToolSearchConfig). When enabled with two or more function tools, requests add OpenAI’s hosted tool_search tool and set defer_loading on functions (with rules for explicit flags and single-tool edge cases). Tool conversion preserves defer_loading from tool metadata. Docs in en/ar/ko/pt-BR document the new parameter.

Execution path: Discovered function calls still run through CrewAI’s existing native loop against the agent’s authorized tool list (not ad-hoc definitions from the model). New test_hosted_tool_execution exercises hosted vs eager loading for success, unknown tools, bad JSON, non-object args (null, arrays, strings), and tool failures.

Argument parsing: parse_tool_call_args now rejects parsed JSON that is not a object before execution, returning the same INVALID_INPUT shape as malformed JSON—fixing cases where null or [] previously ran tools with defaults.

Reviewed by Cursor Bugbot for commit 3c50a6f. Bugbot is set up for automated code reviews on this repo. Configure here.

Add a tool_search option to OpenAICompletion. When enabled on the
Responses API with two or more function tools, the hosted tool_search
tool is added and function tools are sent with defer_loading=True.
An explicit defer_loading flag on a tool is preserved during
conversion. Eager loading stays the default.

Refs crewAIInc#8008
OpenAI rejects a deferred function tool unless tool_search is in the
request. A single tool with defer_loading=True (or any explicitly
deferred tool without the tool_search option) was sent without it.
Tools without the flag are only deferred when tool_search is enabled
and there are two or more of them, as before.
Verify hosted discovery and eager calls through the existing authorized tool catalog, including matching call IDs and error results. Reject non-object JSON arguments before execution instead of falling back to tool defaults.

Refs crewAIInc#8125. Depends on crewAIInc#8040.
@strix-security

strix-security Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 95 commits after the last Strix review (3c50a6f). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ No open security findings.

PR summary: Adds opt-in OpenAI hosted tool search for the Responses API (`tool_search` config, `defer_loading` on function tools, request-side wiring) and makes the shared tool-argument parser reject non-object JSON so malformed arguments can no longer execute a tool with its defaults. Includes docs updates and new fake-client tests for hosted and eager tool execution.

Review summary

I reviewed both production changes in this PR — the tightened parse_tool_call_args in lib/crewai/src/crewai/utilities/agent_utils.py and the new hosted tool-search / defer_loading support in lib/crewai/src/crewai/llms/providers/openai/completion.py (plus the translated docs and the two test files) — and found no security issues introduced by the change.

The parser rewrite is a strict fail-closed improvement: every native tool-execution sink (execute_single_native_tool_call and _execute_single_native_tool_call) goes through it, and malformed or non-container argument payloads now produce a structured tool failure instead of reaching a call with defaulted arguments. The tool-search feature only changes how tools are presented to the provider: it never removes tools from the request, defer_tool_loading cannot be set by tool metadata, model output, or MCP-sourced schemas, and discovered function calls are still resolved by name against the agent's own authorized tool catalog, with unknown names returning "Tool not found". Request-parameter assembly rebuilds tool entries from a fixed key set, so caller- or tool-supplied keys cannot inject provider tool types, and no shared state is mutated.

Three suspect paths were investigated and independently validated as non-issues: the pre-existing {} coercion fallbacks in the provider-level available_functions branches (unchanged, unreachable from the agent loop), the explicit defer_loading path enabling tool search without the config flag (developer-only input, no capability change), and the falsy-argument coalescing in extract_tool_call_info (pre-existing, grants no capability the model does not already have).

Ruled out (3)
  • Can malformed tool arguments still execute a tool with default arguments through the provider-level Responses branches?
    Those `json.JSONDecodeError -> {}` fallbacks are unchanged pre-existing code, are only reachable when a caller supplies its own `available_functions` (the agent loop passes `available_functions=None`), and the PR's new strict parser covers both real execution paths.
  • Does an explicit `defer_loading` flag enable hosted tool search even when the `tool_search` option is disabled?
    Only developer-authored tool dicts can set that flag (framework tool schemas use fixed keys), the request still carries the identical caller-supplied catalog, and execution still resolves function names only against the agent's own tools.
  • Does the `or {}` coalescing in `extract_tool_call_info` bypass the new non-object argument rejection?
    Only falsy values collapse to `{}` and that pre-existing unchanged code grants the model no capability it does not already have, since it can emit arbitrary explicit arguments anyway.

Review 1 on this PR · 3c50a6f · requested Nov 4 21:30 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings

@mintlify

mintlify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit e713344.

Approve preview

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Execute hosted-discovery Responses function calls through the authorized tool catalog

2 participants