Call Agent.train occurs "TypeError: 'coroutine' object is not iterable"

my code is blow:

def train_core(
    domain_file: Text = "domain.yml",
    policies_file: Text = "policies.yml",
    training_data_file: Text = "data/stories.md",
    model_directory: Text = "models",
    model_name: Text = "dialogue",
):
    policies = core_config.load(policies_file)
    agent = Agent(
        domain_file,
        policies=policies,
    )

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

    model_path = os.path.join(model_directory, "core", model_name)
    agent.persist(model_path)

    return model_path

i think the code workflow is no problem,but when it executes agent.train(training_data),error message occurs:

Processed trackers: 0it [00:00, ?it/s]Traceback (most recent call last):
  File "bot.py", line 89, in <module>
    test_agent_and_persist()
  File "bot.py", line 76, in test_agent_and_persist
    agent.train(training_data, validation_split=0.0)
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/agent.py", line 668, in train
    self.policy_ensemble.train(training_trackers, self.domain, **kwargs)
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/policies/ensemble.py", line 89, in train
    policy.train(training_trackers, domain, **kwargs)
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/policies/keras_policy.py", line 172, in train
    training_data = self.featurize_for_training(training_trackers, domain, **kwargs)
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/policies/policy.py", line 78, in featurize_for_training
    training_data = self.featurizer.featurize_trackers(training_trackers, domain)
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/featurizers.py", line 384, in featurize_trackers
    trackers, domain
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/rasa/core/featurizers.py", line 584, in training_states_and_actions
    for tracker in pbar:
  File "/home/sxx/.conda/envs/rasa/lib/python3.7/site-packages/tqdm/_tqdm.py", line 1005, in __iter__
    for obj in iterable:
TypeError: 'coroutine' object is not iterable
1 Like

load_data is an async function, you either need to await it or wrap it in an async loop:

loop = asyncio.get_event_loop()
training_data = loop.run_until_complete(agent.load_data(training_data_file))
agent.train(training_data)
2 Likes

@tmbo I got the same error, when I used the async function I am getting a different error:

from future import absolute_import

from future import division

from future import unicode_literals

import logging

import asyncio

from rasa.core.agent import Agent

from rasa.core.policies.keras_policy import KerasPolicy

from rasa.core.policies.memoization import MemoizationPolicy

from rasa.core.policies.fallback import FallbackPolicy

from rasa.core.tracker_store import TrackerStore

from rasa.core.domain import Domain

from rasa.core.trackers import DialogueStateTracker

from rasa_mongo_tracker_store.store import MongoTrackerStore

from rasa.core.featurizers import (MaxHistoryTrackerFeaturizer, BinarySingleStateFeaturizer)

if name == β€˜main’:

domain = Domain.load('faq_domain.yml')
logging.basicConfig(level='INFO')

training_data_file = './data/sample_stories.md'
model_path = './models/dialogue'

featurizer = MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(),
                                     max_history=2)
fallback = FallbackPolicy(fallback_action_name="action_my_fallback",
                      core_threshold=0.5,
                      nlu_threshold=0.5)
db = MongoTrackerStore(domain,host='mongodb://localhost:27017', database_name="rasa", collection="conversations")
agent = Agent('faq_domain.yml',tracker_store=db, policies = [MemoizationPolicy(max_history=2), KerasPolicy(featurizer), fallback])

loop = asyncio.get_event_loop()
training_data = loop.run_until_complete(agent.load_data(training_data_file))
agent.train(training_data)
        
agent.persist(model_path)

Error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-a903baa11187> in <module>
     31 
     32     loop = asyncio.get_event_loop()
---> 33     training_data = loop.run_until_complete(agent.load_data(training_data_file))
     34     agent.train(training_data)
     35 

/usr/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    458         future.add_done_callback(_run_until_complete_cb)
    459         try:
--> 460             self.run_forever()
    461         except:
    462             if new_task and future.done() and not future.cancelled():

/usr/lib/python3.6/asyncio/base_events.py in run_forever(self)
    412         self._check_closed()
    413         if self.is_running():
--> 414             raise RuntimeError('This event loop is already running')
    415         if events._get_running_loop() is not None:
    416             raise RuntimeError(

RuntimeError: This event loop is already running
Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 13/13 [00:00<00:00, 567.20it/s, # trackers=1]
Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 13/13 [00:00<00:00, 264.30it/s, # trackers=5]
Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 13/13 [00:00<00:00, 252.90it/s, # trackers=5]
Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 13/13 [00:00<00:00, 283.06it/s, # trackers=5]
1 Like

Hey Visam, Try removing the loop variable and make agent.load_data() as an awaitable object.

training_data= await agent.load_data(training_data_file)

either i am getting None Response or typeerror coroutine object not callable

@rockey1228 I am facing the same issue have you found any solution?

i have the same issue ,any updates?

Following worked for me:

training_data = asyncio.run(agent.load_data(training_data_file)) agent.train(training_data)

1 Like

Please try this below code it may will work.

async def train_core(domain_file: Text = β€œdomain.yml”, model_directory: Text = β€œmodels”, model_name: Text = β€œcore”, training_data_file: Text = β€œdata/stories.md”,):

logging.basicConfig(filename=logfile, level=logging.DEBUG)

agent = Agent(
    domain_file,
    policies=[
        MemoizationPolicy(max_history=3),
        KerasPolicy(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)
agent.persist(model_path)

if name == β€˜main’: loop = asyncio.get_event_loop() loop.run_until_complete(train_core())

This worked for me. Thank you very much.

training_data = await agent.load_data(training_data_file)

Hi I am still getting the error , pleas find the details below :slight_smile:

Python : 3.6.9

#Code from future import absolute_import from future import division from future import unicode_literals

import logging import asyncio from rasa.nlu.training_data import load_data

from rasa_core.agent import Agent from rasa_core.policies.keras_policy import KerasPolicy from rasa_core.policies.memoization import MemoizationPolicy

if name == β€˜main’: logging.basicConfig(level=β€˜INFO’)

training_data_file = './data/stories.md'
model_path = './models/dialogue'

agent = Agent('customer_domain.yml', policies = [MemoizationPolicy(max_history = 2),
                                              KerasPolicy(epochs = 500,
                                                          batch_size = 10,
                                                          validation_split = 0.2)])
loop = asyncio.get_event_loop()
training_data = loop.run_until_complete(agent.load_data(training_data_file))
agent.train(training_data)

#agent.train(data)
#data= asyncio.run(agent.load_data(training_data_file))

#agent.train(data)


agent.persist(model_path)

error

RuntimeError: This event loop is already running

Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5/5 [00:00<00:00, 312.70it/s, # trackers=1] Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5/5 [00:00<00:00, 250.10it/s, # trackers=5] Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5/5 [00:00<00:00, 138.92it/s, # trackers=10] Processed Story Blocks: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5/5 [00:00<00:00, 124.79it/s, # trackers=9]

Thank you

Hi All ,

I have managed to solve the above issue , Please find below the following code

from future import absolute_import from future import division from future import unicode_literals

import logging import asyncio from rasa.core.agent import Agent from rasa.core.policies.keras_policy import KerasPolicy from rasa.core.policies.memoization import MemoizationPolicy

if name == β€˜main’: logging.basicConfig(level=β€˜INFO’)

training_data_file = './data/stories.md'
model_path = './models/dialogue'

agent = Agent('customer_domain.yml', policies = [MemoizationPolicy(max_history = 2,), 
                                                 KerasPolicy(epochs = 500,
                                                             batch_size = 10,
                                                             validation_split = 0.2)])

loop = asyncio.get_event_loop()
training_data = await agent.load_data(training_data_file)
agent.train(training_data)

#data = agent.load_data('stories.md')
#agent.train(data)
		

agent.persist(model_path)