> This page is for Server SDKs.

> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes.

# set_expected_answer

> Tell the recognizer how many digits the caller's answer in this step has.

[ref-step]: /docs/server-sdks/reference/python/agents/context-builder/step

[ref-expected-answer]: /docs/server-sdks/reference/python/agents/context-builder/expected-answer

[ref-add-gather-question]: /docs/server-sdks/reference/python/agents/context-builder/step/add-gather-question

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()`][ref-add-gather-question] and
[`expected_answer()`][ref-expected-answer].

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

## Parameters

**`digits`** `int | None` — default: None

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

---

**`min_digits`** `int | None` — default: None

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

---

**`max_digits`** `int | None` — default: None

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

---

## Returns

[`Step`][ref-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

```python {7}
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:

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

### Range

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