Python: OpenAI responses client (#239)

* Responses client WIP

* add responses class

* fix typing errors

* move test

* streaming responses, structured outputs

* tests

* Update python/packages/main/tests/openai/test_openai_responses_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* pr comments

* fix override import

* fix mypy

* add missing function override

* PR comments

* add docstrings

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
peterychang
2025-07-25 13:56:22 -04:00
committed by GitHub
Unverified
parent 233c557173
commit 3ee9dddfa2
7 changed files with 979 additions and 21 deletions
@@ -0,0 +1,36 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from random import randint
from typing import Annotated
from agent_framework.openai import OpenAIResponsesClient
from pydantic import Field
def get_weather(
location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
"""Get the weather for a given location."""
conditions = ["sunny", "cloudy", "rainy", "stormy"]
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
async def main() -> None:
client = OpenAIResponsesClient(ai_model_id="gpt-4o-mini")
message = "What's the weather in Amsterdam and in Paris?"
stream = False
print(f"User: {message}")
if stream:
print("Assistant: ", end="")
async for chunk in client.get_streaming_response(message, tools=get_weather):
if str(chunk):
print(str(chunk), end="")
print("")
else:
response = await client.get_response(message, tools=get_weather)
print(f"Assistant: {response}")
if __name__ == "__main__":
asyncio.run(main())