Python: added user agents to foundry and azure openai (#658)

* added user agents to foundry and azure openai

* improvement

* improvement for disabled setup
This commit is contained in:
Eduard van Valkenburg
2025-09-10 10:57:37 +02:00
committed by GitHub
Unverified
parent 3b81164a6d
commit 947f2bf642
5 changed files with 47 additions and 14 deletions
@@ -28,7 +28,7 @@ from agent_framework import (
)
from agent_framework._pydantic import AFBaseSettings
from agent_framework.exceptions import ServiceInitializationError, ServiceResponseException
from agent_framework.telemetry import use_telemetry
from agent_framework.telemetry import prepend_agent_framework_to_user_agent, use_telemetry
from azure.ai.agents.models import (
AgentsNamedToolChoice,
AgentsNamedToolChoiceType,
@@ -95,6 +95,8 @@ class FoundrySettings(AFBaseSettings):
TFoundryChatClient = TypeVar("TFoundryChatClient", bound="FoundryChatClient")
HEADERS = prepend_agent_framework_to_user_agent()
@use_telemetry
@use_tool_calling
@@ -267,7 +269,11 @@ class FoundryChatClient(BaseChatClient):
raise ServiceInitializationError("Model deployment name is required for agent creation.")
agent_name = self.agent_name
args = {"model": self.ai_model_deployment_name, "name": agent_name}
args = {
"model": self.ai_model_deployment_name,
"name": agent_name,
"headers": HEADERS,
}
if run_options:
if "tools" in run_options:
args["tools"] = run_options["tools"]
@@ -303,7 +309,11 @@ class FoundryChatClient(BaseChatClient):
if thread_run is not None and tool_run_id is not None and tool_run_id == thread_run.id and tool_outputs:
# There's an active run and we have tool results to submit, so submit the results.
await self.client.agents.runs.submit_tool_outputs_stream( # type: ignore[reportUnknownMemberType]
thread_run.thread_id, tool_run_id, tool_outputs=tool_outputs, event_handler=handler
thread_run.thread_id,
tool_run_id,
tool_outputs=tool_outputs,
event_handler=handler,
headers=HEADERS,
)
# Pass the handler to the stream to continue processing
stream = handler # type: ignore
@@ -316,6 +326,7 @@ class FoundryChatClient(BaseChatClient):
stream = await self.client.agents.runs.stream( # type: ignore[reportUnknownMemberType]
final_thread_id,
agent_id=agent_id,
headers=HEADERS,
**run_options,
)
@@ -326,7 +337,9 @@ class FoundryChatClient(BaseChatClient):
if thread_id is None:
return None
async for run in self.client.agents.runs.list(thread_id=thread_id, limit=1, order=ListSortOrder.DESCENDING):
async for run in self.client.agents.runs.list(
thread_id=thread_id, limit=1, order=ListSortOrder.DESCENDING, headers=HEADERS
): # type: ignore[reportUnknownMemberType]
if run.status not in [
RunStatus.COMPLETED,
RunStatus.CANCELLED,
@@ -346,13 +359,14 @@ class FoundryChatClient(BaseChatClient):
messages=run_options["additional_messages"],
tool_resources=run_options.get("tool_resources"),
metadata=run_options.get("metadata"),
headers=HEADERS,
)
run_options["additional_messages"] = []
return thread.id
if thread_run is not None:
# There was an active run; we need to cancel it before starting a new run.
await self.client.agents.runs.cancel(thread_id, thread_run.id)
await self.client.agents.runs.cancel(thread_id, thread_run.id, headers=HEADERS)
return thread_id
@@ -474,7 +488,7 @@ class FoundryChatClient(BaseChatClient):
async def _cleanup_agent_if_needed(self) -> None:
"""Clean up the agent if we created it."""
if self._should_delete_agent and self.agent_id is not None:
await self.client.agents.delete_agent(self.agent_id)
await self.client.agents.delete_agent(self.agent_id, headers=HEADERS)
self.agent_id = None
self._should_delete_agent = False