Skip to content

Commit 8df8f85

Browse files
authored
[aisdk] Allow passing of provider metadata in tool definitions (#579)
## Summary We need it so that we can specify whether the tool schema validation should be strict or not. ## How was it tested? Used with testpilot and its custom tools. I'll send a PR on axiom to show its usage too. ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent b6fb7db commit 8df8f85

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

aisdk/ai/api/llm_tool.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type ToolDefinition interface {
3030
}
3131

3232
// For the equivalent of ToolDefinition in MCP, see the Tool struct in:
33-
// https://github.com/modelcontextprotocol/go-sdk/blob/main/mcp/protocol.go#L854
33+
// https://github.com/modelcontextprotocol/go-sdk/blob/main/mcp/protocol.go#L901
3434

3535
// FunctionTool represents a tool that has a name, description, and set of input arguments.
3636
// Note: this is not the user-facing tool definition. The AI SDK methods will
@@ -47,6 +47,11 @@ type FunctionTool struct {
4747
// the tool's input requirements and provide matching suggestions.
4848
// InputSchema should be defined using a JSON schema.
4949
InputSchema *jsonschema.Schema `json:"input_schema,omitzero"`
50+
51+
// ProviderMetadata contains additional provider-specific metadata.
52+
// They are passed through to the provider from the AI SDK and enable
53+
// provider-specific functionality that can be fully encapsulated in the provider.
54+
ProviderMetadata *ProviderMetadata `json:"provider_metadata,omitzero"`
5055
}
5156

5257
var _ ToolDefinition = &FunctionTool{}
@@ -57,6 +62,9 @@ func (t *FunctionTool) Type() string { return "function" }
5762
// isToolDefinition is a marker method to satisfy the ToolDefinition interface
5863
func (t *FunctionTool) isToolDefinition() bool { return true }
5964

65+
// GetProviderMetadata returns the provider-specific metadata for the function tool
66+
func (t FunctionTool) GetProviderMetadata() *ProviderMetadata { return t.ProviderMetadata }
67+
6068
// FunctionTool JSON marshaling - automatically includes "type" field
6169
func (t *FunctionTool) MarshalJSON() ([]byte, error) {
6270
type Alias FunctionTool

aisdk/ai/provider/openai/internal/codec/encode_tools.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ func encodeFunctionTool(tool api.FunctionTool) (*responses.ToolUnionParam, []api
9393
return nil, nil, fmt.Errorf("failed to convert tool parameters: %w", err)
9494
}
9595

96+
strict := true // Default to true
97+
md := GetMetadata(tool)
98+
if md != nil && md.StrictSchemas != nil {
99+
strict = *md.StrictSchemas
100+
}
101+
96102
result := responses.ToolParamOfFunction(
97103
name,
98104
props,
99-
// TODO: allow passing the strict flag to the function
100-
true, // strict mode enabled
105+
strict,
101106
)
102107

103108
// Add description if provided

0 commit comments

Comments
 (0)