question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: 'coroutine' object is not iterable

See original GitHub issue

Rasa version: rasa 1.0.2
rasa-core-sdk 0.14.0
rasa-sdk 1.0.0

Python version: Python 3.7.3

Operating system (windows, osx, …): Mac osx Mojave

Issue: When I run the below command: python dialogue_management_model.py

The error that pops up is this frame below:

WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0. For more information, please see:

Traceback (most recent call last): File “dialogue_management_model.py”, line 38, in <module> train_dialogue() File “dialogue_management_model.py”, line 25, in train_dialogue agent.train(data) File “/usr/local/lib/python3.7/site-packages/rasa/core/agent.py”, line 668, in train self.policy_ensemble.train(training_trackers, self.domain, **kwargs) File “/usr/local/lib/python3.7/site-packages/rasa/core/policies/ensemble.py”, line 89, in train policy.train(training_trackers, domain, **kwargs) File “/usr/local/lib/python3.7/site-packages/rasa/core/policies/memoization.py”, line 152, in train for t in training_trackers TypeError: ‘coroutine’ object is not iterable sys:1: RuntimeWarning: coroutine ‘Agent.load_data’ was never awaited

I’m unsure of how this is happening, and I saw on another post to add “await” before when i train the agent, but that gave a syntax error. Please help!!

Content of configuration file (config.yml):

{
    "pipeline":"spacy_sklearn",
    "path":"./models/nlu",
    "data":"./data/data.json"
}

Content of domain file (domain.yml):

slots:
  location:
    type: text


intents:
  - greet
  - goodbye
  - inform


entities:
  - location

templates:
  utter_greet:
    - 'Hello! How can I help you today?'
    - 'Hi there!'
  utter_goodbye:
    - 'Goodbye! I hope I was of assistance today :)'
    - 'Bye bye!'
    - 'See you later!'
  utter_ask_location:
    - 'Could you specify the location?'
    - 'In what location?'
    - 'Where, exactly?'


actions:
  - utter_greet
  - utter_goodbye
  - utter_ask_location
  - action_weather

Content of dialogue_management_model.py:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import rasa.core
from rasa.core.agent import Agent
from rasa.core.policies.keras_policy import KerasPolicy
from rasa.core.policies.memoization import MemoizationPolicy
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.core.run import serve_application
from rasa.core import config

logger = logging.getLogger(__name__)

def train_dialogue(domain_file = 'weather_domain.yml',
					model_path = './models/dialogue',
					training_data_file = './data/stories.md'):
					
	agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])
	data = agent.load_data(training_data_file)	
	

	agent.train(data)
	
	agent.persist(model_path)
	return agent
	
def run_weather_bot(serve_forever=True):
	interpreter = RasaNLUInterpreter('./models/nlu/default/weathernlu')
	agent = Agent.load('./models/dialogue', interpreter=interpreter)
	rasa.core.run.serve_application(agent ,channel='cmdline')
		
	return agent
	
if __name__ == '__main__':
	train_dialogue()
	run_weather_bot()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
tabergmacommented, Jun 4, 2019

We use asyncio internally. agent.load_data is a coroutine that needs to be run and waited for. Please try out the following code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import asyncio
import rasa.core
from rasa.core.agent import Agent
from rasa.core.policies.keras_policy import KerasPolicy
from rasa.core.policies.memoization import MemoizationPolicy
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.core.run import serve_application
from rasa.core import config

logger = logging.getLogger(__name__)


async def train_dialogue(domain_file='weather_domain.yml',
                   model_path='./models/dialogue',
                   training_data_file='./data/stories.md'):
    agent = Agent(domain_file, policies=[MemoizationPolicy(),
                                         KerasPolicy(max_history=3, epochs=200,
                                                     batch_size=50)])

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete(agent.load_data(training_data_file))

    agent.train(data)

    agent.persist(model_path)
    return agent


def run_weather_bot(serve_forever=True):
    interpreter = RasaNLUInterpreter('./models/nlu/default/weathernlu')
    agent = Agent.load('./models/dialogue', interpreter=interpreter)
    rasa.core.run.serve_application(agent, channel='cmdline')

    return agent


if __name__ == '__main__':
    train_dialogue()
    run_weather_bot()

As you are using python3.7 you should be able to replace

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete(agent.load_data(training_data_file))

by

    asyncio.run(agent.load_data(training_data_file))
5reactions
tabergmacommented, Jun 14, 2019

@neerajb1 As soon as you put the keyword async to the function definition, it becomes a coroutine. If you not await the coroutine, the error you mentioned will show up.

In your case you can simply remove the keyword async from train_dialogue as it is not needed. Please have a look at https://docs.python.org/3/library/asyncio-task.html#coroutine for more information.

Try the following:

def train_dialogue(
  domain_file = 'domain.yml',
  model_path = 'models/dialogue',
  training_data_file = 'data/stories.md'
):

    agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete( agent.load_data(training_data_file ))

    agent.train(data)
    agent.persist(model_path)
    return agent
Read more comments on GitHub >

github_iconTop Results From Across the Web

'coroutine' object is not iterable` when using compute ... - GitHub
I'm using dask.distributed in a nameko service. When running something closely related to this following code, I get the TypeError in the title....
Read more >
TypeError: 'coroutine' object is not iterable - Stack Overflow
This is my aiohttp Server Setup. import socketio from aiohttp import web import aiohttp_cors # create aiohttp application app = web.Application ...
Read more >
TypeError:'coroutine' object is not iterable - Google Groups
Hi Cong,. Why are you using async and await? Native coroutines are still very new, they were introduced in Python 3.5 and Tornado...
Read more >
'coroutine' object is not iterable [How to Solve] - ProgrammerAH
'coroutine' object is not iterable [How to Solve]. ValueError: [TypeError("'coroutine' object is not iterable"), TypeError('vars() argument ...
Read more >
Call Agent.train occurs "TypeError: 'coroutine' object is not ...
Call Agent.train occurs "TypeError: 'coroutine' object is not iterable" ... Hey Visam, Try removing the loop variable and make agent.load_data() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found