Python: Request cancellation sample (#459)

* request cancellation via tasks

* fix missing kwargs
This commit is contained in:
peterychang
2025-08-20 15:56:26 -04:00
committed by GitHub
Unverified
parent 011edfe420
commit feb4e908ae
3 changed files with 20 additions and 51 deletions
@@ -0,0 +1,19 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework.openai import OpenAIChatClient
async def main():
chat_client = OpenAIChatClient()
try:
task = asyncio.create_task(chat_client.get_response(messages=["Tell me a fantasy story."]))
await asyncio.sleep(1)
task.cancel()
await task
except asyncio.CancelledError:
print("Request was cancelled")
if __name__ == "__main__":
asyncio.run(main())