mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-08-10 16:32:28 +00:00
fix: make Azure Responses OpenAICompletion mock reliable under xdist
Patch AzureCompletion._openai_completion_class instead of the dynamic import target so Responses delegate tests do not intermittently use the real OpenAICompletion and fail under pytest-xdist. Co-authored-by: Rip&Tear <theCyberTech@users.noreply.github.com>
This commit is contained in:
@@ -183,13 +183,24 @@ class AzureCompletion(BaseLLM):
|
||||
pass
|
||||
return self
|
||||
|
||||
@staticmethod
|
||||
def _openai_completion_class() -> Any:
|
||||
"""Return the OpenAICompletion class used for Responses API delegation.
|
||||
|
||||
Isolated so tests can patch this lookup reliably under pytest-xdist
|
||||
instead of racing the dynamic import inside ``_init_responses_delegate``.
|
||||
"""
|
||||
from crewai.llms.providers.openai.completion import OpenAICompletion
|
||||
|
||||
return OpenAICompletion
|
||||
|
||||
def _init_responses_delegate(self) -> None:
|
||||
"""Create an OpenAICompletion delegate for the Azure OpenAI Responses API.
|
||||
|
||||
The Azure OpenAI Responses API uses the standard OpenAI Python SDK
|
||||
with a base_url pointing to the Azure resource's /openai/v1/ endpoint.
|
||||
"""
|
||||
from crewai.llms.providers.openai.completion import OpenAICompletion
|
||||
openai_completion_cls = self._openai_completion_class()
|
||||
|
||||
base_url = self._get_responses_base_url()
|
||||
|
||||
@@ -239,7 +250,7 @@ class AzureCompletion(BaseLLM):
|
||||
if self.additional_params:
|
||||
delegate_kwargs["additional_params"] = self.additional_params
|
||||
|
||||
self._responses_delegate = OpenAICompletion(**delegate_kwargs)
|
||||
self._responses_delegate = openai_completion_cls(**delegate_kwargs)
|
||||
|
||||
def _get_responses_base_url(self) -> str:
|
||||
"""Construct the base URL for the Azure OpenAI Responses API.
|
||||
|
||||
@@ -29,9 +29,12 @@ def azure_env():
|
||||
def mock_openai_completion():
|
||||
"""Mock OpenAICompletion to avoid real client creation.
|
||||
|
||||
Patches at the source module so that the dynamic import inside
|
||||
_init_responses_delegate picks up the mock.
|
||||
Patches ``AzureCompletion._openai_completion_class`` so the Responses
|
||||
delegate lookup is deterministic under pytest-xdist (patching the
|
||||
dynamic import target alone can miss under parallel workers).
|
||||
"""
|
||||
from crewai.llms.providers.azure.completion import AzureCompletion
|
||||
|
||||
instance = MagicMock()
|
||||
instance.call = MagicMock(return_value="responses-result")
|
||||
instance.acall = AsyncMock(return_value="async-responses-result")
|
||||
@@ -41,9 +44,10 @@ def mock_openai_completion():
|
||||
instance.reset_reasoning_chain = MagicMock()
|
||||
mock_cls = MagicMock(return_value=instance)
|
||||
|
||||
with patch(
|
||||
"crewai.llms.providers.openai.completion.OpenAICompletion",
|
||||
mock_cls,
|
||||
with patch.object(
|
||||
AzureCompletion,
|
||||
"_openai_completion_class",
|
||||
return_value=mock_cls,
|
||||
):
|
||||
yield mock_cls, instance
|
||||
|
||||
|
||||
Reference in New Issue
Block a user