Skip to content

feat(openai): support Strict parameter for tool function definitions via ToolInfo.Extra#801

Open
piratf wants to merge 1 commit into
cloudwego:mainfrom
piratf:feat/strict-tool
Open

feat(openai): support Strict parameter for tool function definitions via ToolInfo.Extra#801
piratf wants to merge 1 commit into
cloudwego:mainfrom
piratf:feat/strict-tool

Conversation

@piratf
Copy link
Copy Markdown

@piratf piratf commented Apr 26, 2026

What type of PR is this?

feat

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

feat(openai): 通过 ToolInfo.Extra 支持 Tool Function 的 Strict 参数

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

Background

OpenAI's Structured Outputs feature supports a strict parameter on tool function definitions. When set to true, the model is guaranteed to generate arguments that exactly match the provided JSON Schema — no additional or missing properties, correct types, and valid values.

Currently, the internal functionDefinition struct in libs/acl/openai/tool.go lacks a Strict field, and the toTools() / genRequest() conversion path completely ignores it. There is no way for users to enable strict mode from business code.

Additionally, DeepSeek V4 now supports strict on tool calls as well, meaning this change benefits both OpenAI and DeepSeek users (when using the OpenAI-compatible endpoint).

What this PR does

This is a minimal, backward-compatible change across 3 files:

  1. tool.go: Add Strict bool field to functionDefinition
  2. chat_model.go: Read strict from ToolInfo.Extra["strict"] in toTools(), propagate it through genRequest() to openai.FunctionDefinition.Strict
  3. chat_model_test.go: Add TestToToolsStrict covering strict/non-strict/invalid values and full genRequest propagation

Usage

toolInfo := &schema.ToolInfo{
    Name:        "my_tool",
    Desc:        "description of the tool",
    ParamsOneOf: schema.NewParamsOneOfByJSONSchema(jsonSchema),
    Extra:       map[string]any{"strict": true},
}

Note

After this PR is merged, components/model/openai/go.mod should be updated to reference the new version of libs/acl/openai.

zh(optional):

背景

OpenAI Structured Outputs 支持在 tool function 定义中设置 strict 参数。设为 true 时,模型保证生成的参数严格匹配 JSON Schema。

当前 libs/acl/openai 内部的 functionDefinition 缺少 Strict 字段,toTools()genRequest() 的转换链路也完全忽略了该参数,业务代码无法启用 strict 模式。

此外,DeepSeek V4 现在也支持 tool call 的 strict 参数,因此此改动同时惠及通过 OpenAI 兼容接口使用 DeepSeek 的用户。

改动说明

最小化、向后兼容的改动,共涉及 3 个文件:

  1. tool.go: functionDefinition 增加 Strict 字段
  2. chat_model.go: 从 ToolInfo.Extra["strict"] 读取 bool 值,经 toTools()genRequest() 全链路透传到 openai.FunctionDefinition.Strict
  3. chat_model_test.go: 新增 TestToToolsStrict,覆盖 strict/非 strict/无效值三种场景及 genRequest 完整传播

使用方式

toolInfo := &schema.ToolInfo{
    Name:        "my_tool",
    Desc:        "工具描述",
    ParamsOneOf: schema.NewParamsOneOfByJSONSchema(jsonSchema),
    Extra:       map[string]any{"strict": true},
}

备注

此 PR 合入后,需要更新 components/model/openai/go.mod 中对 libs/acl/openai 的版本引用。

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

Allow setting strict mode on tool functions via ToolInfo.Extra["strict"].
This enables OpenAI Structured Outputs for function calling, ensuring
the model generates exact JSON Schema compliant arguments.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 26, 2026

CLA assistant check
All committers have signed the CLA.

@piratf piratf marked this pull request as ready for review April 26, 2026 11:04
@piratf
Copy link
Copy Markdown
Author

piratf commented Apr 26, 2026

@JonXSnow @meguminnnnnnnnn Hi team, I saw you have recently been working on this package. Could you please help take a look at this change? Thanks!

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants