Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World Example Not Working in Jupyter Notebook #144

Closed
joshcx opened this issue Dec 5, 2024 · 3 comments · Fixed by #214
Closed

Hello World Example Not Working in Jupyter Notebook #144

joshcx opened this issue Dec 5, 2024 · 3 comments · Fixed by #214
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@joshcx
Copy link

joshcx commented Dec 5, 2024

The provided 'Hello World' example in the documentation is still not working.
version: 0.0.9

import dotenv

dotenv.load_dotenv()

agent = Agent(  
    'openai:gpt-4o',
    system_prompt='Be concise, reply with one sentence.',  
)

result = agent.run_sync('Where does "hello world" come from?') 

Error:

python
RuntimeError                              Traceback (most recent call last)
/test_pydanticai.ipynb Cell 2 line 1
      6 agent = Agent(  
      7     'openai:gpt-4o',
      8     system_prompt='Be concise, reply with one sentence.',  
      9 )
     11 import asyncio
13 result = agent.run_sync('Where does "hello world" come from?')  

File ~/miniconda3/lib/python3.11/site-packages/pydantic_ai/agent.py:226, in Agent.run_sync(self, user_prompt, message_history, model, deps)
    212 Run the agent with a user prompt synchronously.
    213 
    214 This is a convenience method that wraps `self.run` with `loop.run_until_complete()`.
   (...)
    223     The result of the run.
    224 
    225 loop = asyncio.get_event_loop()
226 return loop.run_until_complete(self.run(user_prompt, message_history=message_history, model=model, deps=deps))

File ~/miniconda3/lib/python3.11/asyncio/base_events.py:629, in BaseEventLoop.run_until_complete(self, future)
    618"Run until the Future is done.
    619 
    620 If the argument is a coroutine, it is wrapped in a Task.
   (...)
    626 Return the Future's result, or raise its exception.
    627 """
    628 self._check_closed()
 629 self._check_running()
    631 new_task = not futures.isfuture(future)
    632 future = tasks.ensure_future(future, loop=self)

File ~/miniconda3/lib/python3.11/asyncio/base_events.py:588, in BaseEventLoop._check_running(self)
    586 def _check_running(self):
    587     if self.is_running():
 588         raise RuntimeError('This event loop is already running')
    589     if events._get_running_loop() is not None:
    590         raise RuntimeError(
    591             'Cannot run the event loop while another loop is running')

RuntimeError: This event loop is already running
@jonathanbouchet
Copy link

Hi @joshcx ,
it looks like you are running the code in a (jupyter) notebook.

Try the following to run asynchronous code on a notebook (you might pip install nest_asyncio if not already)

import nest_asyncio
nest_asyncio.apply()

@joshcx
Copy link
Author

joshcx commented Dec 5, 2024

Ah thanks that fixes it! Perhaps it'll be helpful to add that to the documentation?

@joshcx joshcx changed the title Hello World Example Not Working Hello World Example Not Working in Jupyter Notebook Dec 5, 2024
@samuelcolvin
Copy link
Member

yup, let's add a note to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants