@@ -1704,3 +1704,301 @@ def test_parse_chunk_surfaces_oauth_consent_requested_event() -> None:
17041704 assert consent_contents [0 ].consent_link == "https://consent-host.example.com/authorize?code=xyz"
17051705 assert update .role == "assistant"
17061706 assert update .raw_representation is mock_event
1707+
1708+
1709+ def test_client_model_not_set_from_openai_chat_model (monkeypatch : pytest .MonkeyPatch ) -> None :
1710+ """client.model must be empty when OPENAI_CHAT_MODEL is set."""
1711+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1712+
1713+ mock_project = MagicMock ()
1714+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1715+
1716+ client = RawFoundryAgentChatClient (
1717+ project_client = mock_project ,
1718+ agent_name = "my-prompt-agent" ,
1719+ )
1720+
1721+ assert client .model != "gpt-4.1-from-env" , "client.model must not be sourced from OPENAI_CHAT_MODEL"
1722+ assert not client .model , f"Expected empty/falsy model, got { client .model !r} "
1723+
1724+
1725+ def test_agent_default_options_not_polluted_by_openai_chat_model (
1726+ monkeypatch : pytest .MonkeyPatch ,
1727+ ) -> None :
1728+ """Agent.default_options['model'] must not be the env-var value."""
1729+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1730+
1731+ mock_project = MagicMock ()
1732+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1733+
1734+ client = RawFoundryAgentChatClient (
1735+ project_client = mock_project ,
1736+ agent_name = "my-prompt-agent" ,
1737+ )
1738+ agent = Agent (client = cast (Any , client ), instructions = "test" )
1739+
1740+ model_in_options = agent .default_options .get ("model" )
1741+ assert model_in_options != "gpt-4.1-from-env" , (
1742+ f"default_options['model'] must not be 'gpt-4.1-from-env', got { model_in_options !r} "
1743+ )
1744+ assert not model_in_options , f"default_options['model'] must be falsy (empty or absent), got { model_in_options !r} "
1745+
1746+
1747+ def test_client_model_not_set_from_openai_model (monkeypatch : pytest .MonkeyPatch ) -> None :
1748+ """client.model must be empty when OPENAI_MODEL is set (no OPENAI_CHAT_MODEL)."""
1749+ monkeypatch .delenv ("OPENAI_CHAT_MODEL" , raising = False )
1750+ monkeypatch .setenv ("OPENAI_MODEL" , "generic-gpt-from-env" )
1751+
1752+ mock_project = MagicMock ()
1753+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1754+
1755+ client = RawFoundryAgentChatClient (
1756+ project_client = mock_project ,
1757+ agent_name = "my-prompt-agent" ,
1758+ )
1759+
1760+ assert client .model != "generic-gpt-from-env" , "client.model must not be sourced from OPENAI_MODEL"
1761+ assert not client .model , f"Expected empty/falsy model, got { client .model !r} "
1762+
1763+
1764+ def test_raw_foundry_agent_chat_client_rejects_model_keyword_arg () -> None :
1765+ """RawFoundryAgentChatClient.__init__ does not accept a 'model' parameter."""
1766+ mock_project = MagicMock ()
1767+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1768+
1769+ cls = cast (Any , RawFoundryAgentChatClient )
1770+ with pytest .raises (TypeError , match = "unexpected keyword argument" ):
1771+ cls (
1772+ project_client = mock_project ,
1773+ agent_name = "my-prompt-agent" ,
1774+ model = "gpt-5.4" ,
1775+ )
1776+
1777+
1778+ def test_explicit_model_in_agent_default_options_survives (
1779+ monkeypatch : pytest .MonkeyPatch ,
1780+ ) -> None :
1781+ """An explicit model in Agent(default_options=...) must survive into default_options."""
1782+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1783+
1784+ mock_project = MagicMock ()
1785+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1786+
1787+ client = RawFoundryAgentChatClient (
1788+ project_client = mock_project ,
1789+ agent_name = "my-prompt-agent" ,
1790+ )
1791+ agent = Agent (
1792+ client = cast (Any , client ),
1793+ instructions = "test" ,
1794+ default_options = cast (Any , {"model" : "gpt-5.4" }),
1795+ )
1796+
1797+ assert agent .default_options .get ("model" ) == "gpt-5.4" , (
1798+ f"Explicit model override must survive in default_options, got { agent .default_options .get ('model' )!r} "
1799+ )
1800+
1801+
1802+ def test_explicit_model_in_as_agent_default_options_survives (
1803+ monkeypatch : pytest .MonkeyPatch ,
1804+ ) -> None :
1805+ """An explicit model passed to client.as_agent(default_options=...) survives."""
1806+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1807+
1808+ mock_project = MagicMock ()
1809+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1810+
1811+ client = RawFoundryAgentChatClient (
1812+ project_client = mock_project ,
1813+ agent_name = "my-prompt-agent" ,
1814+ )
1815+ agent = client .as_agent (
1816+ instructions = "test" ,
1817+ default_options = cast (Any , {"model" : "gpt-5.4" }),
1818+ )
1819+
1820+ assert agent .default_options .get ("model" ) == "gpt-5.4" , (
1821+ f"Explicit model in as_agent default_options must survive, got { agent .default_options .get ('model' )!r} "
1822+ )
1823+
1824+
1825+ def test_allow_preview_client_model_not_set_from_env (monkeypatch : pytest .MonkeyPatch ) -> None :
1826+ """allow_preview=True clients must also not inherit OPENAI_CHAT_MODEL env var into client.model."""
1827+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1828+
1829+ mock_project = MagicMock ()
1830+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1831+
1832+ client = RawFoundryAgentChatClient (
1833+ project_client = mock_project ,
1834+ agent_name = "hosted-agent" ,
1835+ allow_preview = True ,
1836+ )
1837+
1838+ assert client .model != "gpt-4.1-from-env" , (
1839+ "Hosted-agent client (allow_preview=True) must not inherit OPENAI_CHAT_MODEL"
1840+ )
1841+ assert not client .model , f"Expected empty/falsy model, got { client .model !r} "
1842+
1843+
1844+ def test_allow_preview_binds_agent_name_to_get_openai_client () -> None :
1845+ """When allow_preview=True, agent_name is passed to get_openai_client for endpoint binding."""
1846+ mock_project = MagicMock ()
1847+ mock_project .get_openai_client .return_value = MagicMock ()
1848+
1849+ client = RawFoundryAgentChatClient (
1850+ project_client = mock_project ,
1851+ agent_name = "hosted-agent" ,
1852+ allow_preview = True ,
1853+ )
1854+
1855+ mock_project .get_openai_client .assert_called_once_with (agent_name = "hosted-agent" )
1856+ assert client .allow_preview is True
1857+
1858+
1859+ async def test_allow_preview_get_response_executes_successfully_with_empty_model (
1860+ monkeypatch : pytest .MonkeyPatch ,
1861+ ) -> None :
1862+ """End-to-end get_response test on allow_preview=True with OPENAI_CHAT_MODEL set in env."""
1863+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1864+
1865+ mock_response = MagicMock ()
1866+ mock_response .id = "resp_123"
1867+ mock_response .model = "test-model"
1868+ mock_response .created_at = 1000000000
1869+ mock_response .output_parsed = None
1870+ mock_response .metadata = {}
1871+ mock_response .output = []
1872+ mock_response .usage = None
1873+ mock_response .finish_reason = None
1874+ mock_response .conversation = None
1875+ mock_response .status = "completed"
1876+ mock_response .parse .return_value = mock_response
1877+ mock_response .headers = {}
1878+
1879+ mock_openai = MagicMock ()
1880+ mock_openai .responses .with_raw_response .create = AsyncMock (return_value = mock_response )
1881+ mock_project = MagicMock ()
1882+ mock_project .get_openai_client .return_value = mock_openai
1883+
1884+ client = RawFoundryAgentChatClient (
1885+ project_client = mock_project ,
1886+ agent_name = "hosted-agent" ,
1887+ allow_preview = True ,
1888+ )
1889+
1890+ assert client .model == ""
1891+
1892+ response = await client .get_response ([Message (role = "user" , contents = ["Hello hosted agent" ])])
1893+
1894+ assert response .response_id == "resp_123"
1895+
1896+ create_call = mock_openai .responses .with_raw_response .create .await_args
1897+ assert create_call is not None
1898+ kwargs = create_call .kwargs
1899+
1900+ assert kwargs .get ("model" ) != "gpt-4.1-from-env"
1901+ assert "extra_body" not in kwargs or "agent_reference" not in kwargs .get ("extra_body" , {})
1902+
1903+
1904+ def test_azure_openai_chat_model_does_not_leak (monkeypatch : pytest .MonkeyPatch ) -> None :
1905+ """AZURE_OPENAI_CHAT_MODEL must not leak into client.model."""
1906+ monkeypatch .delenv ("OPENAI_CHAT_MODEL" , raising = False )
1907+ monkeypatch .delenv ("OPENAI_MODEL" , raising = False )
1908+ monkeypatch .setenv ("AZURE_OPENAI_CHAT_MODEL" , "azure-gpt-4.1-from-env" )
1909+
1910+ mock_project = MagicMock ()
1911+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1912+
1913+ client = RawFoundryAgentChatClient (
1914+ project_client = mock_project ,
1915+ agent_name = "my-prompt-agent" ,
1916+ )
1917+
1918+ assert client .model != "azure-gpt-4.1-from-env" , "client.model must not be sourced from AZURE_OPENAI_CHAT_MODEL"
1919+ assert not client .model , f"Expected empty/falsy model, got { client .model !r} "
1920+
1921+
1922+ def test_azure_openai_model_does_not_leak (monkeypatch : pytest .MonkeyPatch ) -> None :
1923+ """AZURE_OPENAI_MODEL must not leak into client.model."""
1924+ monkeypatch .delenv ("OPENAI_CHAT_MODEL" , raising = False )
1925+ monkeypatch .delenv ("OPENAI_MODEL" , raising = False )
1926+ monkeypatch .setenv ("AZURE_OPENAI_MODEL" , "azure-model-from-env" )
1927+
1928+ mock_project = MagicMock ()
1929+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1930+
1931+ client = RawFoundryAgentChatClient (
1932+ project_client = mock_project ,
1933+ agent_name = "my-prompt-agent" ,
1934+ )
1935+
1936+ assert client .model != "azure-model-from-env" , "client.model must not be sourced from AZURE_OPENAI_MODEL"
1937+ assert not client .model , f"Expected empty/falsy model, got { client .model !r} "
1938+
1939+
1940+ async def test_env_model_is_stripped_from_agent_reference_payload (
1941+ monkeypatch : pytest .MonkeyPatch ,
1942+ ) -> None :
1943+ """Even if '' survives into default_options, it must not appear in the API payload."""
1944+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1945+
1946+ mock_project = MagicMock ()
1947+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1948+
1949+ client = RawFoundryAgentChatClient (
1950+ project_client = mock_project ,
1951+ agent_name = "my-prompt-agent" ,
1952+ agent_version = "1.0" ,
1953+ )
1954+
1955+ with patch (
1956+ "agent_framework_openai._chat_client.RawOpenAIChatClient._prepare_options" ,
1957+ new_callable = AsyncMock ,
1958+ return_value = {
1959+ "model" : "" ,
1960+ "input" : [{"role" : "user" , "content" : [{"type" : "input_text" , "text" : "hi" }]}],
1961+ },
1962+ ):
1963+ result = await client ._prepare_options (
1964+ messages = [Message (role = "user" , contents = ["hi" ])],
1965+ options = {},
1966+ )
1967+
1968+ assert "model" not in result or not result .get ("model" ), (
1969+ f"Empty/falsy model must be stripped from outgoing payload, got model={ result .get ('model' )!r} "
1970+ )
1971+
1972+
1973+ async def test_continuation_turn_strips_empty_model (
1974+ monkeypatch : pytest .MonkeyPatch ,
1975+ ) -> None :
1976+ """Multi-turn continuation calls with conversation_id='resp_123' must also strip empty model strings."""
1977+ monkeypatch .setenv ("OPENAI_CHAT_MODEL" , "gpt-4.1-from-env" )
1978+
1979+ mock_project = MagicMock ()
1980+ mock_project .get_openai_client .return_value = MagicMock (spec = AsyncOpenAI )
1981+
1982+ client = RawFoundryAgentChatClient (
1983+ project_client = mock_project ,
1984+ agent_name = "my-prompt-agent" ,
1985+ agent_version = "1.0" ,
1986+ )
1987+
1988+ with patch (
1989+ "agent_framework_openai._chat_client.RawOpenAIChatClient._prepare_options" ,
1990+ new_callable = AsyncMock ,
1991+ return_value = {
1992+ "model" : "" ,
1993+ "previous_response_id" : "resp_123" ,
1994+ "input" : [{"role" : "user" , "content" : [{"type" : "input_text" , "text" : "hi" }]}],
1995+ },
1996+ ):
1997+ result = await client ._prepare_options (
1998+ messages = [Message (role = "user" , contents = ["hi" ])],
1999+ options = {"conversation_id" : "resp_123" },
2000+ )
2001+
2002+ assert "model" not in result or not result .get ("model" ), (
2003+ f"Empty/falsy model must be stripped on continuation turns, got model={ result .get ('model' )!r} "
2004+ )
0 commit comments