You are viewing documentation for our open source project which is maintained by the community. If you want to get started building assistants with Rasa please check out our latest [documentation here](/content/docs/index.html).

**Don't overuse rules**. Rules are great to handle small specific conversation patterns, but unlike [stories](https://legacy-docs-oss.rasa.com/docs/rasa/2.x/stories), rules don't have the power to generalize to unseen conversation paths. Combine rules and stories to make your assistant robust and able to handle real user behavior.

If you can't decide whether to write a story or a rule to implement a certain behavior, see the best practices for [Writing Conversation Data](https://legacy-docs-oss.rasa.com/docs/rasa/2.x/writing-stories).

For additional examples on implementing rules in a Rasa assistant, refer to our [Rules example bot](https://github.com/RasaHQ/rasa/tree/main/examples/rules).

## Writing a Rule

Before you start writing rules, you have to make sure that the [Rule Policy](https://legacy-docs-oss.rasa.com/docs/rasa/2.x/policies#rule-policy) is added to your model configuration:

```yaml
policies:
  - ... # Other policies
  - name: RulePolicy
```

Rules can then be added to the `rules` section of your training data.

To indicate that a rule can apply at any point in a conversation, start with the intent which starts the conversation and then add the actions which your assistant should perform in response to that.

```yaml
rules:
  - rule: Say `hello` whenever the user sends a message with intent `greet`
    steps:
      - intent: greet
      - action: utter_greet
```

This example rule applies at the start of conversation as well as when the user decides to send a message with an intent `greet` in the middle of an ongoing conversation.

Dialogue turns that only appear as rules in the training data and do not appear in stories will be ignored by ML-only policies like `TEDPolicy` at prediction time.

```yaml
rules:
  - rule: Say `hello` whenever the user sends a message with intent `greet`
    steps:
      - intent: greet
      - action: utter_greet
```

```yaml
stories:
  - story: story to find a restaurant
    steps:
      - intent: find_restaurant
      - action: restaurant_form
      - action: utter_restaurant_found
```

For example if you define the greeting rule as above and don't add it to any of your stories, after `RulePolicy` predicts `utter_greet`, `TEDPolicy` will make predictions as if the `greet, utter_greet` turn did not occur.

##### Changed in 2.5

The ML-only policies behavior of ignoring rule-only dialogue turns at prediction time was added.

### Rules for the Conversation Start

To write a rule which only applies at the beginning of a conversation, add a `conversation_start: true` to your rule:

```yaml
rules:
  - rule: Say `hello` when the user starts a conversation with intent `greet`
    conversation_start: true
    steps:
      - intent: greet
      - action: utter_greet
```

If a user sends a message with the intent `greet` later in the conversation, the rule will not match.

### Rules with Conditions

Conditions describe requirements which have to be fulfilled for a rule to be applicable. To do so, add any information about the prior conversation under the `condition` key:

```yaml
rules:
  - rule: Only say `hello` if the user provided a name
    condition:
      - slot_was_set:
          - user_provided_name: true
    steps:
      - intent: greet
      - action: utter_greet
```

Possible information that you can include under `condition` includes `slot_was_set` events and `active_loop` events.

### Skip Waiting for User Input at the End of a Rule

By default, rules will wait for the next user message when finished with the last step:

```yaml
rules:
  - rule: Rule which will wait for user message when it was applied
    steps:
      - intent: greet
      - action: utter_greet
      # - action: action_listen
      # Every rule implicitly includes a prediction for `action_listen` as last step.
      # This means that Rasa Open Source will wait for the next user message.
```

If you want to hand over the next action prediction to another story or rule, add `wait_for_user_input: false` to your rule:

```yaml
rules:
  - rule: Rule which will not wait for user message once it was applied
    steps:
      - intent: greet
      - action: utter_greet
        wait_for_user_input: false
```

This indicates that the assistant should execute another action before waiting for more user input.

### Rules and Forms

When a [Form](https://legacy-docs-oss.rasa.com/docs/rasa/2.x/forms) is active, the bot will make predictions based on how the form is defined, ignoring rules. Rules become applicable again if:

- the form fills all required slots
- the form rejects its execution (see [Handling Unhappy Paths](https://legacy-docs-oss.rasa.com/docs/rasa/2.x/forms#writing-stories--rules-for-unhappy-form-paths) for more details)
