Skip to content

Example: deterministic input/tool-call guard via on_user_turn_completed and ToolError #6269

Description

@eeee2345

Feature Type

Nice to have

Feature Description

The docs already point to on_user_turn_completed for editing or dropping a user message before it reaches the LLM, and to llm_node for filtering generated content. What's missing is a runnable examples/ reference that shows a deterministic, code-level guard wired into those hooks, rather than relying on system-prompt instructions to hold the line (as the hotel_receptionist safety scenarios do today).

Concretely, an example under examples/ that:

  • in on_user_turn_completed, scans new_message and either strips the offending content or raises StopResponse() when a rule matches, and
  • inside a @function_tool, validates arguments and raises ToolError(...) when a tool call looks unsafe.

The detection itself can be any pattern engine — the value of the example is the wiring at the hook boundary, which carries over to voice/realtime agents where there's no separate proxy to inspect text. I'd use ATR (an open MIT detection ruleset with a small Python engine) for the matching, but the example would keep it swappable so it reads as a pattern, not a dependency.

If this seems in scope I'm happy to open a PR adding it under examples/, following the existing example layout and make check/make fix.

Disclosure: I maintain ATR.

Workarounds / Alternatives

Today the available patterns are documented (on_user_turn_completed, llm_node) but there's no end-to-end example, so users wire it up ad hoc. The hotel_receptionist scenarios cover safety through natural-language instructions, which is a different mechanism than a deterministic check.

Additional Context

Scope is intentionally small: one self-contained example, no framework changes, no new dependency in the core package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions