Skip to navigation

set_expected_answer

View as MarkdownOpen in Claude

Tell the recognizer how many digits the caller’s answer in this step has, such as a ZIP code or a PIN. The recognizer stops waiting once the caller has said that many digits.

While a gather question with its own expected_answer is being asked, that value replaces the step’s. See add_gather_question() and expected_answer().

Pass digits for an exact count, or both min_digits and max_digits for a range.

Parameters

digits
int | NoneDefaults to None

The exact number of digits, from 1 to 64. Can’t be combined with min_digits or max_digits.

min_digits
int | NoneDefaults to None

Keyword-only. The fewest digits, from 1 to 64. Requires max_digits.

max_digits
int | NoneDefaults to None

Keyword-only. The most digits, from 1 to 64, and at least min_digits. Requires min_digits.

Returns

Step — Self for method chaining.

Raises

ValueError for a count outside 1 to 64, a min_digits greater than max_digits, digits combined with a range, or a range with only one end.

Examples

Exact count

from signalwire import AgentBase
agent = AgentBase(name="zip", route="/zip")
agent.prompt_add_section("Role", "You look up stores.")
ctx = agent.define_contexts().add_context("default")
ctx.add_step("collect").set_text("Ask for the caller's ZIP code.").set_expected_answer(digits=5)
agent.serve()

The step renders as:

{"name": "collect", "text": "Ask for the caller's ZIP code.", "expected_answer": {"digits": 5}}

Range

step.set_expected_answer(min_digits=4, max_digits=6)