Python
fix(llm): pass the output-token cap and a configured endpoint to Azure OpenAI (#1584, #1600)
Yijia-Xiao347974a000b6
diff --git a/tests/test_azure_client.py b/tests/test_azure_client.pynew file mode 100644index 000000000..9252b04ac--- /dev/null+++ b/tests/test_azure_client.py@@ -0,0 +1,28 @@+"""Azure OpenAI takes the run's settings like every other provider."""++from __future__ import annotations++import pytest++from tradingagents.llm_clients.factory import create_llm_client++[email protected](autouse=True)+def _azure_env(monkeypatch):+ monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-key")+ monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "https://from-env.openai.azure.com/")+ monkeypatch.setenv("OPENAI_API_VERSION", "2025-03-01-preview")+ monkeypatch.delenv("AZURE_OPENAI_DEPLOYMENT_NAME", raising=False)+++def test_the_output_token_cap_reaches_the_deployment():+ llm = create_llm_client("azure", "gpt-5.5", max_tokens=1234).get_llm()+ assert llm.max_tokens == 1234+++def test_an_explicit_endpoint_wins_over_the_environment():+ llm = create_llm_client("azure", "gpt-5.5", base_url="https://deep.openai.azure.com/").get_llm()+ assert llm.azure_endpoint == "https://deep.openai.azure.com/"+ assert create_llm_client("azure", "gpt-5.5").get_llm().azure_endpoint == "https://from-env.openai.azure.com/"diff --git a/tradingagents/llm_clients/azure_client.py b/tradingagents/llm_clients/azure_client.pyindex f6f996edd..199d421bf 100644--- a/tradingagents/llm_clients/azure_client.py+++ b/tradingagents/llm_clients/azure_client.py@@ -6,7 +6,7 @@ from langchain_openai import AzureChatOpenAIfrom .base_client import BaseLLMClient, normalize_content_PASSTHROUGH_KWARGS = (- "timeout", "max_retries", "api_key", "reasoning_effort", "temperature",+ "timeout", "max_retries", "api_key", "reasoning_effort", "temperature", "max_tokens","callbacks", "http_client", "http_async_client",)@@ -23,7 +23,8 @@ class AzureOpenAIClient(BaseLLMClient):Requires environment variables:AZURE_OPENAI_API_KEY: API key- AZURE_OPENAI_ENDPOINT: Endpoint URL (e.g. https://<resource>.openai.azure.com/)+ AZURE_OPENAI_ENDPOINT: Endpoint URL (e.g. https://<resource>.openai.azure.com/),+ unless backend_url gives oneAZURE_OPENAI_DEPLOYMENT_NAME: Deployment nameOPENAI_API_VERSION: API version (e.g. 2025-03-01-preview)"""@@ -39,6 +40,8 @@ class AzureOpenAIClient(BaseLLMClient):"model": self.model,"azure_deployment": os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", self.model),}+ if self.base_url: # a configured endpoint, else AZURE_OPENAI_ENDPOINT+ llm_kwargs["azure_endpoint"] = self.base_urlfor key in _PASSTHROUGH_KWARGS:if key in self.kwargs: