You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cannot find any example on how to use streaming text chunks in NON-async context
trying something like this:
importasynciofromdatetimeimportdatetimefrompydantic_aiimportAgentdefget_event_loop():
try:
event_loop=asyncio.get_event_loop()
exceptRuntimeError:
event_loop=asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)
returnevent_loopagent=Agent(
'openai:gpt-4o',
system_prompt=(
'You are are a helpful assistant that always answer with too many words loosing some times context but finally getting the right answer.''Also always call tool get_current_time to find the current time to know if answer make sense.''Basically be a bit annoying but always correct. it must be at least three paragraphs long.'
),
)
@agent.tooldefget_current_time(ctx) ->str:
"""Get the current time."""print(f' --> call get_current_time <--')
returnstr(datetime.now())
asyncdefagent_stream_deltas(agent):
asyncwithagent.run_stream('What is the capital of the UK?') asresponse:
asyncforchunkinresponse.stream_text(delta=True):
yieldchunkdefagent_stream_sync(agent):
loop=get_event_loop()
gen=agent_stream_deltas(agent)
whileTrue:
try:
chunk=loop.run_until_complete(gen.__anext__())
yieldchunkexceptStopAsyncIteration:
passif__name__=='__main__':
forchunkinagent_stream_sync(agent):
print(chunk, end='', flush=True)
it prints chunks as they arrive - but et the end crashes/freezes:
--> call get_current_time <--
The capital of the United Kingdom
,...
. The city is an exuberant mix of old and new, tradition and innovation, known for its diverse communities and vibrant urban fabric. Therefore, while my explanation may have taken you on a slightly winding path filled with relevant details, London stands firmly as the city's heart and soul of the United Kingdom.Failed to detach context\
Traceback (most recent call last):
File "/private/tmp/aa_differencingly_kusti/.venv/lib/python3.12/site-packages/opentelemetry/context/__init__.py", line 155, in detach
_RUNTIME_CONTEXT.detach(token)
File "/private/tmp/aa_differencingly_kusti/.venv/lib/python3.12/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach
self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x1020d87c0> at 0x104550c00> was created in a different Context
Additional Context
No response
The text was updated successfully, but these errors were encountered:
The reason is that agent.run_stream opens the span and relies on with to ensure it closes nicely. That doesn't happen in the last case because it's in a generator that gets suspended when the loop breaks and never resumes. Instead it gets closed by garbage collection in a different context, hence the error.
The error is harmless, but obviously distracting and a bit scary-looking, so we're going to do two things:
Stop creating a logfire span unless necessary. This will make your example work without any issues, but it's obviously not a complete solution because it'd still affect those using logfire, which we want to be everyone.
Natively support running PydanticAI in a synchronous context, so you don't have to manually do things like loop.run_until_complete(gen.__anext__()): Feature Request: Synchronous Calls #934
@alexmojaki I think you mentioned you're going to look at 1, and @Kludex is already looking into 2.
Question
Cannot find any example on how to use streaming text chunks in NON-async context
trying something like this:
it prints chunks as they arrive - but et the end crashes/freezes:
Additional Context
No response
The text was updated successfully, but these errors were encountered: