LangEx.Tool.Annotation (LangEx v0.11.3)

Copy Markdown View Source

Annotates tool results with error recovery guidance for the LLM.

After each tool execution round, inspects the results and appends human-readable guidance: what went wrong, how to recover, and what to try instead. Uses JSON-aware error detection rather than naive string matching.

Usage

# As a post-tool-execution step in a graph:
annotations = LangEx.Tool.Annotation.annotate(state.messages)
# => %{messages: [...updated...]} or %{}

Options

  • :empty_threshold — byte size below which a result is considered empty (default 200)
  • :large_threshold — byte size above which a result triggers a "large result" note (default 50_000)
  • :error_detectorfn(content) -> boolean() (default: JSON error key detection)
  • :guidance_builderfn(content) -> String.t() | nil for custom error guidance

Summary

Functions

Annotate the latest tool results in the message list.

Detect whether a tool result contains a JSON error payload.

Default error recovery guidance builder.

Extract the latest consecutive tool result messages from a message list.

Check whether the latest tool results contain substantive (non-empty, non-error) data.

Functions

annotate(messages, opts \\ [])

@spec annotate(
  [LangEx.Message.t()],
  keyword()
) :: %{optional(:messages) => [LangEx.Message.t()]}

Annotate the latest tool results in the message list.

Returns %{messages: updated_messages} if annotations were added, or %{} if no annotations are needed.

default_error_detector(content)

@spec default_error_detector(String.t()) :: boolean()

Detect whether a tool result contains a JSON error payload.

Returns true if the content parses as JSON with an "error" or "errors" top-level key.

default_guidance(content)

@spec default_guidance(String.t()) :: String.t()

Default error recovery guidance builder.

Inspects the error message and returns context-specific recovery advice.

latest_tool_results(messages)

@spec latest_tool_results([LangEx.Message.t()]) :: [LangEx.Message.Tool.t()]

Extract the latest consecutive tool result messages from a message list.

tool_results_substantive?(messages, opts \\ [])

@spec tool_results_substantive?(
  [LangEx.Message.t()],
  keyword()
) :: boolean()

Check whether the latest tool results contain substantive (non-empty, non-error) data.