pydantic-ai icon indicating copy to clipboard operation
pydantic-ai copied to clipboard

Hello World Example Not Working in Jupyter Notebook

Open joshcx opened this issue 2 months ago • 3 comments

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

joshcx avatar Dec 05 '24 06:12 joshcx