Repository navigation
feat(server-utils): Send gen_ai.tool.definitions in the documented format #26445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sentry-junior
wants to merge
5
commits into
develop
Choose a base branch
from
feat/gen-ai-tool-definitions-format
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e37c8ef
feat(server-utils): Send gen_ai.tool.definitions in the documented fo…
sentry-junior[bot] 13c0684
Merge remote-tracking branch 'origin/develop' into feat/gen-ai-tool-d…
sentry-junior[bot] 5db8a8a
Merge remote-tracking branch 'origin/develop' into feat/gen-ai-tool-d…
sentry-junior[bot] 3b37818
Merge remote-tracking branch 'origin/develop' into feat/gen-ai-tool-d…
sentry-junior[bot] 7a75892
Merge remote-tracking branch 'origin/develop' into feat/gen-ai-tool-d…
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { debug, isObjectLike } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../../debug-build'; | ||
|
|
||
| /** A `gen_ai.tool.definitions` entry in the shape documented by the Sentry conventions. */ | ||
| interface GenAiToolDefinition { | ||
| type: string; | ||
| name: string; | ||
| description?: string; | ||
| parameters?: unknown; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes provider tool definitions as the `{ type, name, description, parameters }` entries of | ||
| * `gen_ai.tool.definitions`, or returns `undefined` if there are none. | ||
| * | ||
| * Function tools come in several shapes: OpenAI Chat Completions and Mistral nest them under | ||
| * `function`, Anthropic puts the schema in `input_schema`, Bedrock in `inputSchema.json`, Vercel AI | ||
| * in `inputSchema`, Google in `parameters` or `parametersJsonSchema`. A bare string is a tool name. | ||
| * Tools that a provider runs itself (such as OpenAI's `web_search`) have no schema and are kept as they are. | ||
| */ | ||
| export function getToolDefinitionsAttribute(tools: unknown): string | undefined { | ||
| const definitions = Array.isArray(tools) ? tools.map(toGenAiToolDefinition).filter(tool => tool !== undefined) : []; | ||
| if (!definitions.length) { | ||
| return undefined; | ||
| } | ||
| try { | ||
| return JSON.stringify(definitions); | ||
| } catch (error) { | ||
| DEBUG_BUILD && debug.error('Failed to serialize tool definitions:', error); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function toGenAiToolDefinition(tool: unknown): unknown { | ||
| if (typeof tool === 'string') { | ||
| return { type: 'function', name: tool }; | ||
| } | ||
| if (!isObjectLike(tool)) { | ||
| return undefined; | ||
| } | ||
| // Anthropic marks its function tools as `custom`, to tell them apart from its server tools. | ||
| const isFunctionTool = | ||
| tool.type === undefined || tool.type === 'function' || (tool.type === 'custom' && 'input_schema' in tool); | ||
| if (!isFunctionTool) { | ||
| return tool; | ||
| } | ||
| const fn = isObjectLike(tool.function) ? tool.function : tool; | ||
| if (typeof fn.name !== 'string') { | ||
| return undefined; | ||
| } | ||
| const definition: GenAiToolDefinition = { type: 'function', name: fn.name }; | ||
| if (typeof fn.description === 'string') { | ||
| definition.description = fn.description; | ||
| } | ||
| const parameters = getToolParameters(fn); | ||
| if (parameters !== undefined) { | ||
| definition.parameters = parameters; | ||
| } | ||
| return definition; | ||
| } | ||
|
|
||
| function getToolParameters(fn: Record<string, unknown>): unknown { | ||
| const inputSchema = | ||
| isObjectLike(fn.inputSchema) && isObjectLike(fn.inputSchema.json) ? fn.inputSchema.json : fn.inputSchema; | ||
| const schema = fn.parameters ?? fn.input_schema ?? inputSchema ?? fn.parametersJsonSchema ?? fn.schema; | ||
| // A Zod schema serializes to its internals, not to a JSON schema. | ||
| if (!isObjectLike(schema) || '_def' in schema || '_zod' in schema) { | ||
| return undefined; | ||
| } | ||
| return schema; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chained map and filter loops
Low Severity
The SDK performance convention asks to avoid chaining
.mapand.filterover the same array; a classicforloop is the expected replacement.getToolDefinitionsAttributewalkstoolstwice this way. I flagged this because it is called out in the PR review rules.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 3b37818. Configure here.