Skip to content

Conversation

@arturoliduena
Copy link
Contributor

Closes #234193

Summary

Fixes an issue where the chat/complete stream could unexpectedly terminate when the LLM attempted to call a tool that resulted in one of the following errors:

  • The tool was not available (e.g., "Tool 'execute_query' called but was not available").

  • The tool was called with invalid or missing arguments.

Instead of terminating the stream, these errors are now captured and returned as part of the tool response, allowing the chat to continue normally.

@arturoliduena arturoliduena requested a review from a team as a code owner October 31, 2025 09:57
@arturoliduena arturoliduena added release_note:fix Team:Obs AI Assistant Observability AI Assistant backport:version Backport to applied version labels v9.2.0 labels Oct 31, 2025
@botelastic botelastic bot added the ci:project-deploy-observability Create an Observability project label Oct 31, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/obs-ai-assistant (Team:Obs AI Assistant)

@github-actions
Copy link
Contributor

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@pmoust pmoust requested a review from Copilot November 3, 2025 20:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors error handling for tool/function calls in the Observability AI Assistant by migrating from custom "function not found" errors to using standardized error types from the @kbn/inference-plugin. The changes also add comprehensive test coverage for error scenarios when tools are called incorrectly.

Key Changes

  • Replaces custom FunctionNotFoundError with ToolNotFoundError from @kbn/inference-plugin
  • Adds new FunctionArgsValidationError to handle invalid function arguments
  • Removes obsolete error handling operators (fail_on_non_existing_function_call.ts, catch_function_limit_exceeded_error.ts)
  • Adds extensive test coverage for scenarios where the LLM calls non-existent tools or tools with invalid arguments

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
complete.spec.ts Adds comprehensive test suites for tool calling error scenarios (non-existent tools and invalid arguments)
tsconfig.json Adds trailing comma to dependencies array for consistency
catch_function_limit_exceeded_error.ts Deleted - functionality consolidated elsewhere
fail_on_non_existing_function_call.ts Deleted - replaced by standardized error handling
continue_conversation.ts Migrates to createToolNotFoundError and adds handling for FunctionArgsValidationError
catch_function_not_found_error.ts Updates to use isToolNotFoundError from @kbn/inference-common and returns structured message instead of empty observable
index.ts Removes unused failOnNonExistingFunctionCall operator
chat_function_client/index.ts Replaces custom FunctionArgsValidationError class with standardized createFunctionArgsValidationError
common/index.ts Removes export of deprecated createFunctionNotFoundError
conversation_complete.ts Replaces FunctionNotFoundError with FunctionArgsValidationError and adds helper functions

"@kbn/inference-endpoint-plugin",
"@kbn/spaces-utils",
"@kbn/usage-collection-plugin"
"@kbn/usage-collection-plugin",
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The trailing comma after @kbn/usage-collection-plugin has been added, but there's an inconsistency: the last item in a JSON array should not have a trailing comma according to JSON specification. While TypeScript config files (which use JSON5) allow trailing commas, this addition should be removed to maintain strict JSON compatibility.

Suggested change
"@kbn/usage-collection-plugin",
"@kbn/usage-collection-plugin"

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

I was about to yell at the bot but it's actually right. We don't have trailing commas in JSON files (it's invalid JSON in most parsers)

TokenLimitReachedError = 'tokenLimitReachedError',
FunctionNotFoundError = 'functionNotFoundError',
FunctionLimitExceededError = 'functionLimitExceededError',
FunctionArgsValidationError = 'functionArgsValidationError',
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the inference plugin handling this? Do we need our own error codes for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @sorenlouv, I have removed FunctionArgsValidationError and used instead isToolValidationError from @kbn/inference-common

});
});

describe('when the LLM calls a function that is not available', function () {
Copy link
Member

Choose a reason for hiding this comment

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

You used the work "tool" above but "function" here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, I did have a mix of functions and tools.

…tionError with createToolValidationError and update related tests for tool request terminology.
@elasticmachine
Copy link
Contributor

elasticmachine commented Nov 4, 2025

💔 Build Failed

  • Buildkite Build
  • Commit: c20bea3
  • Kibana Serverless Image: docker.elastic.co/kibana-ci/kibana-serverless:pr-241425-c20bea3d8893

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #21 / chatFunctionClient when executing a function with invalid arguments throws an error
  • [job] [logs] Jest Tests #21 / chatFunctionClient when executing a function with invalid arguments throws an error
  • [job] [logs] FTR Configs #59 / Stateful Observability - Deployment-agnostic APM API integration tests APM alerts error count threshold alert create rule without kql filter produces one alert for each of the opbeans-java and opbeans-php

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
observabilityAIAssistant 486 484 -2

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
observabilityAIAssistant 46.8KB 46.8KB -48.0B
Unknown metric groups

API count

id before after diff
observabilityAIAssistant 488 486 -2

ESLint disabled in files

id before after diff
observabilityAIAssistant 2 1 -1

Total ESLint disabled count

id before after diff
observabilityAIAssistant 9 8 -1

History

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

Labels

backport:version Backport to applied version labels ci:project-deploy-observability Create an Observability project release_note:fix Team:Obs AI Assistant Observability AI Assistant v8.19.0 v9.1.0 v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Obs AI Assistant] Improved error handling for tool response

4 participants