LangChain.Trajectory.Assertions (LangChain v0.8.13)

Copy Markdown View Source

ExUnit assertion helpers for trajectory comparison.

Usage

use LangChain.Trajectory.Assertions

test "agent calls the right tools" do
  trajectory = Trajectory.from_chain(chain)

  assert_trajectory trajectory, [
    %{name: "search", arguments: %{"query" => "weather"}},
    %{name: "get_forecast", arguments: nil}
  ]
end

test "agent does not call dangerous tool" do
  trajectory = Trajectory.from_chain(chain)

  refute_trajectory trajectory, [
    %{name: "delete_all", arguments: nil}
  ], mode: :superset
end

Summary

Functions

Assert that tool name_a was called before tool name_b.

Assert that a trajectory matches the expected tool call sequence.

Assert that tool name_a was NOT called before tool name_b.

Assert that a trajectory does NOT match the expected tool call sequence.

Functions

assert_called_before(actual, name_a, name_b, opts \\ [])

(macro)

Assert that tool name_a was called before tool name_b.

Wraps LangChain.Trajectory.called_before?/4 and accepts the same options:

Examples

assert_called_before trajectory, "search", "answer"
assert_called_before trajectory, "search", "answer", require_both: true

assert_trajectory(actual, expected, opts \\ [])

(macro)

Assert that a trajectory matches the expected tool call sequence.

Accepts the same options as LangChain.Trajectory.matches?/3:

  • :mode:strict (default), :unordered, :superset
  • :args:exact (default), :subset

On failure, raises ExUnit.AssertionError with a diff showing expected vs actual tool calls.

refute_called_before(actual, name_a, name_b, opts \\ [])

(macro)

Assert that tool name_a was NOT called before tool name_b.

Accepts the same options as assert_called_before/4. Note that with the default options a missing tool makes this pass vacuously; pass require_both: true to surface a missing tool as an error instead.

Examples

refute_called_before trajectory, "write_file", "read_file"

refute_trajectory(actual, expected, opts \\ [])

(macro)

Assert that a trajectory does NOT match the expected tool call sequence.

Useful for verifying that specific tools were not called or that a particular call pattern did not occur.

Accepts the same options as assert_trajectory/3.

On failure, raises ExUnit.AssertionError indicating an unexpected match.