From 97766b3c582a4dd19d4ee2b0dc624708955f1040 Mon Sep 17 00:00:00 2001 From: lorenzejay Date: Wed, 21 Jan 2026 16:00:32 -0800 Subject: [PATCH] fixing azure tests --- .../tests/agents/test_native_tool_calling.py | 34 ++++++++- ..._azure_agent_with_native_tool_calling.yaml | 75 +++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 lib/crewai/tests/cassettes/agents/TestAzureNativeToolCalling.test_azure_agent_with_native_tool_calling.yaml diff --git a/lib/crewai/tests/agents/test_native_tool_calling.py b/lib/crewai/tests/agents/test_native_tool_calling.py index ad0b625ef..7ad528ef7 100644 --- a/lib/crewai/tests/agents/test_native_tool_calling.py +++ b/lib/crewai/tests/agents/test_native_tool_calling.py @@ -295,9 +295,41 @@ class TestAzureNativeToolCalling: "AZURE_API_BASE": "https://test.openai.azure.com", "AZURE_API_VERSION": "2024-02-15-preview", } - with patch.dict(os.environ, env_vars): + # Only patch if keys are not already in environment + if "AZURE_API_KEY" not in os.environ: + with patch.dict(os.environ, env_vars): + yield + else: yield + @pytest.mark.vcr() + def test_azure_agent_with_native_tool_calling( + self, calculator_tool: CalculatorTool + ) -> None: + """Test Azure agent can use native tool calling.""" + agent = Agent( + role="Math Assistant", + goal="Help users with mathematical calculations", + backstory="You are a helpful math assistant.", + tools=[calculator_tool], + llm=LLM(model="azure/gpt-4o-mini"), + verbose=False, + max_iter=3, + ) + + task = Task( + description="Calculate what is 15 * 8", + expected_output="The result of the calculation", + agent=agent, + ) + + crew = Crew(agents=[agent], tasks=[task]) + result = crew.kickoff() + + assert result is not None + assert result.raw is not None + assert "120" in str(result.raw) + def test_azure_agent_kickoff_with_tools_mocked( self, calculator_tool: CalculatorTool ) -> None: diff --git a/lib/crewai/tests/cassettes/agents/TestAzureNativeToolCalling.test_azure_agent_with_native_tool_calling.yaml b/lib/crewai/tests/cassettes/agents/TestAzureNativeToolCalling.test_azure_agent_with_native_tool_calling.yaml new file mode 100644 index 000000000..834cd0a23 --- /dev/null +++ b/lib/crewai/tests/cassettes/agents/TestAzureNativeToolCalling.test_azure_agent_with_native_tool_calling.yaml @@ -0,0 +1,75 @@ +interactions: +- request: + body: '{"messages": [{"role": "system", "content": "You are Math Assistant. You + are a helpful math assistant.\nYour personal goal is: Help users with mathematical + calculations"}, {"role": "user", "content": "\nCurrent Task: Calculate what + is 15 * 8\n\nThis is the expected criteria for your final answer: The result + of the calculation\nyou MUST return the actual complete content as the final + answer, not a summary.\n\nThis is VERY important to you, your job depends on + it!"}], "stream": false, "stop": ["\nObservation:"]}' + headers: + Accept: + - application/json + Connection: + - keep-alive + Content-Length: + - '515' + Content-Type: + - application/json + User-Agent: + - X-USER-AGENT-XXX + accept-encoding: + - ACCEPT-ENCODING-XXX + api-key: + - X-API-KEY-XXX + authorization: + - AUTHORIZATION-XXX + x-ms-client-request-id: + - X-MS-CLIENT-REQUEST-ID-XXX + method: POST + uri: https://crewai-azure-openai.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-12-01-preview + response: + body: + string: '{"choices":[{"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"protected_material_code":{"filtered":false,"detected":false},"protected_material_text":{"filtered":false,"detected":false},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":"stop","index":0,"logprobs":null,"message":{"annotations":[],"content":"The + result of the calculation 15 * 8 is 120.","refusal":null,"role":"assistant"}}],"created":1769036451,"id":"chatcmpl-D0bTvDUjQTmRAQ9TYYRtbWkHHF0M5","model":"gpt-4o-mini-2024-07-18","object":"chat.completion","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"jailbreak":{"filtered":false,"detected":false},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}],"system_fingerprint":"fp_f97eff32c5","usage":{"completion_tokens":15,"completion_tokens_details":{"accepted_prediction_tokens":0,"audio_tokens":0,"reasoning_tokens":0,"rejected_prediction_tokens":0},"prompt_tokens":91,"prompt_tokens_details":{"audio_tokens":0,"cached_tokens":0},"total_tokens":106}} + + ' + headers: + Content-Length: + - '1256' + Content-Type: + - application/json + Date: + - Wed, 21 Jan 2026 23:00:51 GMT + Strict-Transport-Security: + - STS-XXX + apim-request-id: + - APIM-REQUEST-ID-XXX + azureml-model-session: + - AZUREML-MODEL-SESSION-XXX + x-accel-buffering: + - 'no' + x-content-type-options: + - X-CONTENT-TYPE-XXX + x-ms-client-request-id: + - X-MS-CLIENT-REQUEST-ID-XXX + x-ms-deployment-name: + - gpt-4o-mini + x-ms-rai-invoked: + - 'true' + x-ms-region: + - X-MS-REGION-XXX + x-ratelimit-limit-requests: + - X-RATELIMIT-LIMIT-REQUESTS-XXX + x-ratelimit-limit-tokens: + - X-RATELIMIT-LIMIT-TOKENS-XXX + x-ratelimit-remaining-requests: + - X-RATELIMIT-REMAINING-REQUESTS-XXX + x-ratelimit-remaining-tokens: + - X-RATELIMIT-REMAINING-TOKENS-XXX + x-request-id: + - X-REQUEST-ID-XXX + status: + code: 200 + message: OK +version: 1