Skip to content

Add gen_ai.tool.definitions to chat model observations - #7275

Open
Ajax-1 wants to merge 2 commits into
spring-projects:mainfrom
Ajax-1:feat/tool-definitions-observation-7260
Open

Ajax-1 wants to merge 2 commits into
spring-projects:mainfrom
Ajax-1:feat/tool-definitions-observation-7260

Conversation

@Ajax-1

@Ajax-1 Ajax-1 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7260

Chat model observations only exposed the names of the tools offered to the model (spring.ai.model.request.tool.names), so a trace did not show the descriptions and parameter schemas the model received, nor anything about tools that were offered but never invoked.

Changes

  • New ToolDefinitionsSerializer that writes tool definitions as a JSON array following the OpenTelemetry tool definitions schema. It is independent of the chat model observation, so it can be reused by other observations that record gen_ai.tool.definitions.
  • New ChatModelToolDefinitionsObservationFilter, following the existing ToolCallingContentObservationFilter pattern, which records the request's tool callbacks as the gen_ai.tool.definitions high cardinality attribute.
  • REQUEST_TOOL_DEFINITIONS added to AiObservationAttributes and ChatModelObservationDocumentation.HighCardinalityKeyNames.
  • Configuration, all false by default:
    • spring.ai.chat.observations.include-tool-definitions enables the attribute, since the conventions mark it as opt-in.
    • spring.ai.chat.observations.include-tool-definition-descriptions and spring.ai.chat.observations.include-tool-definition-parameters include the descriptions and parameter schemas. The conventions do not recommend populating these optional properties by default, as they can be large, so without them each tool is recorded with its type and name only:
      [{"type": "function", "name": "getWeather"}]
      and with both enabled:
      [{"type": "function", "name": "getWeather", "description": "...", "parameters": {"type": "object", ...}}]
    A tool whose input schema is not valid JSON is still described, without its parameters.
  • Observability docs: the new attribute and properties.

Like spring.ai.model.request.tool.names, it covers the tools passed as toolCallbacks in the request options; provider-native server tools are not included.

Scope

#7260 also lists invoke_agent and fetch_response spans, where those operations are instrumented. Spring AI does not have them today, so this PR covers the inference (chat {model}) span. Once invoke_agent observations exist (#7343), they can record the attribute with the same serializer.

Tests

  • ToolDefinitionsSerializerTests: types and names only, with descriptions, with parameters, invalid input schema, empty list.
  • ChatModelToolDefinitionsObservationFilterTests: unsupported context, no tools, options without tool calling, default output, full output, idempotency.
  • ChatObservationAutoConfigurationTests: the filter bean by default, enabled and disabled, and the optional properties.

Chat model observations only exposed the names of the tools offered to
the model, through spring.ai.model.request.tool.names, so a trace did
not show the descriptions and parameter schemas the model received,
nor anything about tools that were offered but never invoked.

Add a ChatModelToolDefinitionsObservationFilter that records the
definitions of the tools in the request as the gen_ai.tool.definitions
attribute, a JSON array following the OpenTelemetry tool definitions
schema (type, name, description and parameters). A tool whose input
schema is not valid JSON is still described, without its parameters.

As the definitions can be large, and the OpenTelemetry conventions
make the attribute opt-in, the filter is only registered when
spring.ai.chat.observations.include-tool-definitions is true.

Fixes spring-projects#7260

Signed-off-by: Ajax-1 <85230126+Ajax-1@users.noreply.github.com>
The OpenTelemetry conventions do not recommend populating the optional
properties of gen_ai.tool.definitions by default, as they can be
large, and let instrumentations provide a way to enable them.

Only record the type and name of each tool by default, and add the
spring.ai.chat.observations.include-tool-definition-descriptions and
spring.ai.chat.observations.include-tool-definition-parameters
properties to include the descriptions and parameter schemas.

Move the serialization to a ToolDefinitionsSerializer, so that other
observations recording the tool definitions can reuse it.

See spring-projects#7260

Signed-off-by: Ajax-1 <85230126+Ajax-1@users.noreply.github.com>
@Ajax-1
Ajax-1 force-pushed the feat/tool-definitions-observation-7260 branch from db2ab4c to 3637a45 Compare November 27, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support gen_ai.tool.definitions on GenAI spans

2 participants