# Buggy code
async def train_core(
    domain_file: Text = "domain.yml",
    model_directory: Text = "models",
    model_name: Text = "current",
    training_data_file: Text = "data/stories.md",
):
    agent = Agent(
        domain_file,
        policies=[
            MemoizationPolicy(max_history=3),
            MappingPolicy(),
            RestaurantPolicy(batch_size=100, epochs=400, validation_split=0.2),
        ],
    )

    training_data = await agent.load_data(training_data_file)
    agent.train(training_data)

    # Attention: agent.persist stores the model and all meta data into a folder.
    # The folder itself is not zipped.
    model_path = os.path.join(model_directory, model_name, "core")
    agent.persist(model_path)

    logger.info("Model trained. Stored in '{}'.".format(model_path))

    return model_path


# Traceback (KeyError)
2019-07-09 10:56:21 INFO     rasa.nlu.test  - Confusion matrix, without normalization:
[[1 0]
[1 0]]
Traceback (most recent call last):
File "/Users/a1451195/anaconda3/bin/rasa", line 10, in <module>
sys.exit(main())
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/__main__.py", line 70, in main
cmdline_arguments.func(cmdline_arguments)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/cli/test.py", line 147, in test
test_core(args)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/cli/test.py", line 85, in test_core
kwargs=vars(args),
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/test.py", line 102, in test_core
rasa.core.test(stories, _agent, out_directory=output, **kwargs)
File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/test.py", line 514, in test
log_failed_stories(story_evaluation.failed_stories, out_directory)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/test.py", line 470, in log_failed_stories
f.write(failure.export_stories())
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/trackers.py", line 434, in export_stories
return story.as_story_string(flat=True, e2e=e2e)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/training/structures.py", line 376, in as_story_string
story_content += step.as_story_string(flat, e2e)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/training/structures.py", line 183, in as_story_string
result += self._user_string(s, e2e)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/training/structures.py", line 137, in _user_string
return "* {}{}\n".format(prefix, story_step_element.as_story_string(e2e))
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/test.py", line 171, in as_story_string
correct_message = md_format_message(self.text, self.intent, self.entities)
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/core/events/__init__.py", line 58, in md_format_message
"entities": deserialised_entities,
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/nlu/training_data/formats/markdown.py", line 293, in _generate_message_md
entities = sorted(message.get("entities", []), key=lambda k: k["start"])
File "/Users/a1451195/anaconda3/lib/python3.7/site-packages/rasa/nlu/training_data/formats/markdown.py", line 293, in <lambda>
entities = sorted(message.get("entities", []), key=lambda k: k["start"])
KeyError: 'start'

# Fixed code
async def train_core(
    domain_file: Text = "domain.yml",
    model_directory: Text = "models",
    model_name: Text = "current",
    training_data_file: Text = "data/stories.md",
):
    agent = Agent(
        domain_file,
        policies=[
            MemoizationPolicy(max_history=3),
            MappingPolicy(),
            RestaurantPolicy(batch_size=100, epochs=100, validation_split=0.2),
        ],
    )

    training_data = await agent.load_data(training_data_file, augmentation_factor=10)
    agent.train(training_data)

    # Attention: agent.persist stores the model and all meta data into a folder.
    # The folder itself is not zipped.
    model_path = os.path.join(model_directory, model_name, "core")
    agent.persist(model_path)

    logger.info("Model trained. Stored in '{}'.".format(model_path))

    return model_path