Skip to content

Align gen_ai.tool_definitions with JSON Schema and add it to completion hook #4181

Open
wikaaaaa wants to merge 25 commits intoopen-telemetry:mainfrom
wikaaaaa:tooldefinitions-completionhook
Open

Align gen_ai.tool_definitions with JSON Schema and add it to completion hook #4181
wikaaaaa wants to merge 25 commits intoopen-telemetry:mainfrom
wikaaaaa:tooldefinitions-completionhook

Conversation

@wikaaaaa
Copy link
Contributor

@wikaaaaa wikaaaaa commented Feb 9, 2026

Description

This PR aligns gen_ai.tool_definitions with JSON Schema proposed in open-telemetry/semantic-conventions#3378.

This PR also adds gen_ai.tool_definitions to completion hook and configures exporting the attribute to GCS. Its configured the same way as system instructions, it's being hashed, since it might be large and often doesn't differ.

Its a continuation of #4142, which added gen_ai.tool_definitions to gen_ai.client.inference.operation.details.

Type of change

New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Manual tests: run adk web and inspected the traces and data send to the storage bucket
  • Unit tests

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@wikaaaaa wikaaaaa changed the title Add gen_ai.tool_definitions to completion hook Align gen_ai.tool_definitions with JSON Schema and add it to completion hook Feb 17, 2026
@wikaaaaa wikaaaaa force-pushed the tooldefinitions-completionhook branch from c79def6 to c6531c5 Compare February 17, 2026 14:03
@wikaaaaa wikaaaaa force-pushed the tooldefinitions-completionhook branch from c6531c5 to a7bc9de Compare February 17, 2026 14:06
## Unreleased
- Fix bug in how tokens are counted when using the streaming `generateContent` method. ([#4152](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4152)).
- Add `gen_ai.tool.definitions` attribute to `gen_ai.client.inference.operation.details` log event ([#4142](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4142)).
- Add `gen_ai.tool_definitions` to completion hook ([#4181](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4181))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we have to bump the util-genai dependency in pyproject.toml: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation-genai/opentelemetry-instrumentation-google-genai/pyproject.toml#L43 -- but I guess we have to do a release of the util gen AI library first ? I'm a bit confused about what the process is supposed to be here @aabmass

if isinstance(td, FunctionToolDefinition):
result.append(
FunctionToolDefinition(
name=td.name,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this could just be 1 long dict/list comprehension

**completion_details_attributes,
}
else:
tool_def_without_params_attr = _tool_def_without_parameters_attr(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just inline this below instead of storing in a variable for 1 line (here and below)


name: str
description: str | None
parameters: Any | None # or dict[str, Any]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure Any includes None ?


## Unreleased

- Add `gen_ai.tool_definitions` to completion hook ([#4181](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4181))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is duplicated here and below

usedforsecurity=False,
).hexdigest()

tool_definitions_hash = None
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: update hash_tool_definitions to accept an empty list and return None in that case in order to make this code a little cleaner..

self._base_path,
f"{system_instruction_hash or uuid_str}_system_instruction.{self._format}",
),
tool_definitions_ref=posixpath.join(
Copy link
Contributor

Choose a reason for hiding this comment

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

do we still want to stamp the ref / do an upload if tool definitions is None ?

return None
return []

tool_definitions = []
Copy link
Contributor

Choose a reason for hiding this comment

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

nit I think it could be a list comprehension ? return [de for de in _to_tool_definition(tool) for tool in _config_to_tools(config) if de (maybe also below, but not sure with the await call..)



def _tool_def_without_parameters_attr(
tool_def: list[ToolDefinition],
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we just want to use FunctionToolDefinitiondirectly Instead of using ToolDefinition everywhere ? I don't think we want to use the GenericToolDefinition at all ?

return {
"error": f"failed to serialize tool definition, tool type={type(tool).__name__}"
}
return [
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see we have a Generic tool definition here.. So do we really want to add something to the telemetry in case of a tool or params that we fail to serialize ? I feel like it makes more sense to leave it out and log an error/warning instead. Maybe worth bringing up that question in the sem conv PR. Maybe @aabmass has thoughts on this

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.

9 participants

Comments