Files
agent-framework/python/packages/lab/lightning/tests/test_lightning.py
Eduard van Valkenburg 5e056b672e Python: [BREAKING] Python: Provider-leading client design & OpenAI package extraction (#4818)
* Python: Provider-leading client design & OpenAI package extraction

Major refactoring of the Python Agent Framework client architecture:

- Extract OpenAI clients into new `agent-framework-openai` package
- Core package no longer depends on openai, azure-identity, azure-ai-projects
- Rename clients for discoverability: OpenAIResponsesClient → OpenAIChatClient,
  OpenAIChatClient → OpenAIChatCompletionClient
- Unify `model_id`/`deployment_name`/`model_deployment_name` → `model` param
- New FoundryChatClient for Azure AI Foundry Responses API
- New FoundryAgent/FoundryAgentClient for connecting to pre-configured Foundry agents
- Remove OpenAIBase/OpenAIConfigMixin from non-deprecated client MRO
- Deprecate AzureOpenAI* clients, AzureAIClient, OpenAIAssistantsClient
- Reorganize samples: azure_openai+azure_ai+azure_ai_agent → azure/
- ADR-0020: Provider-Leading Client Design

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

* fix: missing Agent imports in samples, .model_id → .model in foundry_local sample

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

* fix: CI failures — mypy errors, coverage targets, sample imports

- azure-ai mypy: add type ignores for TypedDict total=, model arg, forward ref
- Coverage: replace core.azure/openai targets with openai package target
- project_provider: add type annotation for opts dict

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

* fix: populate openai .pyi stub, fix broken README links, coverage targets

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

* fixes

* updated observabilitty

* reset azure init.pyi

* fix errors

* updated adr number

* fix foundry local

* fixed not renamed docstrings and comments, and added deprecated markers to old classes

* fix tests and pyprojects

* fix test vars

* updated function tests

* update durable

* updated test setup for functions

* Fix Foundry auth in workflow samples

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

* Stabilize Python integration workflows

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

* Update hosting samples for Foundry

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

* Trigger full CI rerun

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

* Trigger CI rerun again

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

* trigger rerun

* trigger rerun

* fix for litellm

* undo durabletask changes

* Move Foundry APIs into foundry namespace

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

* Fix Foundry pyproject formatting

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

* Split provider samples by Foundry surface

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

* Restore hosting sample requirements

Also fix the Foundry Local sample link after the provider sample move.

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

* updated tests

* udpated foundry integration tests

* removed dist from azurefunctions tests

* Use separate Foundry clients for concurrent agents

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

* fix client setup in azfunc and durable

* disabled two tests

* updated setup for some function and durable tests

* improved azure openai setup with new clients

* ignore deprecated

* fixes

* skip 11

* remove openai assistants int tests

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-25 09:56:29 +00:00

164 lines
5.5 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
"""Tests for lightning module."""
# ruff: noqa
from unittest.mock import AsyncMock, patch
import pytest
from agent_framework import AgentExecutor, AgentResponse, Agent, WorkflowBuilder, Workflow
from agent_framework.openai import OpenAIChatCompletionClient
from openai.types.chat import ChatCompletion, ChatCompletionMessage
from openai.types.chat.chat_completion import Choice
@pytest.fixture
def workflow_two_agents():
"""Test a workflow with two OpenAI chat agents where first agent's result passes to second agent."""
# Mock OpenAI responses
first_agent_response = ChatCompletion(
id="chatcmpl-123",
object="chat.completion",
created=1677652288,
model="gpt-4o",
choices=[
Choice(
index=0,
message=ChatCompletionMessage(role="assistant", content="Analyzed data shows trend upward"),
finish_reason="stop",
)
],
)
second_agent_response = ChatCompletion(
id="chatcmpl-456",
object="chat.completion",
created=1677652289,
model="gpt-4o",
choices=[
Choice(
index=0,
message=ChatCompletionMessage(
role="assistant",
content="Based on the analysis 'Analyzed data shows trend upward', I recommend investing",
),
finish_reason="stop",
)
],
)
# Create mock OpenAI clients
with patch.dict(
"os.environ",
{
"OPENAI_API_KEY": "test-key",
"OPENAI_MODEL": "gpt-4o",
},
):
first_chat_client = OpenAIChatCompletionClient()
second_chat_client = OpenAIChatCompletionClient()
# Mock the OpenAI API calls
with (
patch.object(
first_chat_client.client.chat.completions,
"create",
new_callable=AsyncMock,
return_value=first_agent_response,
),
patch.object(
second_chat_client.client.chat.completions,
"create",
new_callable=AsyncMock,
return_value=second_agent_response,
),
):
# Create the two agents
analyzer_agent = Agent(
client=first_chat_client,
name="DataAnalyzer",
instructions="You are a data analyst. Analyze the given data and provide insights.",
)
advisor_agent = Agent(
client=second_chat_client,
name="InvestmentAdvisor",
instructions="You are an investment advisor. Based on analysis results, provide recommendations.",
)
analyzer_executor = AgentExecutor(id="analyzer", agent=analyzer_agent)
advisor_executor = AgentExecutor(id="advisor", agent=advisor_agent)
# Build workflow: analyzer -> advisor
workflow = (
WorkflowBuilder(start_executor=analyzer_executor).add_edge(analyzer_executor, advisor_executor).build()
)
yield workflow
async def test_openai_workflow_two_agents(workflow_two_agents: Workflow):
events = await workflow_two_agents.run("Please analyze the quarterly sales data")
# Get all output events with AgentResponse
agent_outputs = [event.data for event in events if event.type == "output" and isinstance(event.data, AgentResponse)]
# Check that we have outputs from both agents
assert len(agent_outputs) == 2
assert any("Analyzed data shows trend upward" in str(output) for output in agent_outputs)
assert any(
"Based on the analysis 'Analyzed data shows trend upward', I recommend investing" in str(output)
for output in agent_outputs
)
@pytest.mark.resource_intensive
async def test_observability(workflow_two_agents: Workflow):
r"""Expected trace tree:
[workflow.run]
/ \
[analyzer] [advisor]
/ \ / \
[DataAnalyzer] [send] [Investment] [send]
| |
[chat gpt-4o] [chat gpt-4o]
"""
pytest.importorskip("agentlightning")
from agent_framework_lab_lightning import AgentFrameworkTracer
from agentlightning.adapter import TracerTraceToTriplet
tracer = AgentFrameworkTracer()
try:
tracer.init()
tracer.init_worker(0)
async with tracer.trace_context():
await workflow_two_agents.run("Please analyze the quarterly sales data")
triplets = TracerTraceToTriplet(agent_match=None, llm_call_match="chat").adapt(tracer.get_last_trace())
assert len(triplets) == 2
triplets = TracerTraceToTriplet(agent_match="analyzer", llm_call_match="chat").adapt(tracer.get_last_trace())
assert len(triplets) == 1
triplets = TracerTraceToTriplet(agent_match="advisor", llm_call_match="chat").adapt(tracer.get_last_trace())
assert len(triplets) == 1
# Parent agent is not matched
triplets = TracerTraceToTriplet(agent_match="DataAnalyzer", llm_call_match="chat").adapt(
tracer.get_last_trace()
)
assert len(triplets) == 0
triplets = TracerTraceToTriplet(agent_match="InvestmentAdvisor|advisor", llm_call_match="chat").adapt(
tracer.get_last_trace()
)
assert len(triplets) == 1
finally:
tracer.teardown_worker(0)
tracer.teardown()