From f0685a312a13a5559be029510006f086917aaaa9 Mon Sep 17 00:00:00 2001 From: Weiqing Yang Date: Mon, 18 May 2026 22:25:03 -0700 Subject: [PATCH] [hotfix] Move chat-model os.environ mutations out of module scope in e2e tests Same collection-time pollution pattern that PR #686 fixed for OLLAMA_EMBEDDING_MODEL exists for the chat-model side env vars (OLLAMA_CHAT_MODEL, TONGYI_CHAT_MODEL, OPENAI_CHAT_MODEL, AZURE_OPENAI_CHAT_MODEL, AZURE_OPENAI_API_VERSION) in four e2e test files: e2e_tests_resource_cross_language/chat_model_cross_language_test.py e2e_tests_resource_cross_language/yaml_cross_language_test.py e2e_tests_integration/chat_model_integration_test.py e2e_tests_integration/react_agent_test.py tools/ut.sh runs `pytest flink_agents -k "not e2e_tests"`, and -k only filters which tests execute (not which files are collected). pytest imports every matching file during the collection phase, so any module-scope os.environ mutation in an e2e file leaks into the rest of the test process. Removed the module-scope assignments and moved them into the test functions via monkeypatch.setenv (one of three approaches @wenjin272 listed in PR #686 review), which preserves runtime propagation to the Flink subprocess while auto-restoring after each test. In chat_model_integration_test.py also converted the already-inside-function `os.environ["MODEL_PROVIDER"] = model_provider` to monkeypatch.setenv for consistency, since the parametrize iterations would otherwise leak MODEL_PROVIDER between runs. Reported by @wenjin272 in https://github.com/apache/flink-agents/pull/686#issuecomment-4474060000 ("we can fix the issues with the Open and Ollama scripts in this PR alone, and submit a separate PR to address all environment variable leakage issues"). PR #686 addressed the OLLAMA_EMBEDDING_MODEL side; this PR covers the chat-model side. --- .../chat_model_integration_test.py | 16 +++++++++------- .../e2e_tests_integration/react_agent_test.py | 9 ++++++--- .../chat_model_cross_language_test.py | 6 ++++-- .../yaml_cross_language_test.py | 6 ++++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/python/flink_agents/e2e_tests/e2e_tests_integration/chat_model_integration_test.py b/python/flink_agents/e2e_tests/e2e_tests_integration/chat_model_integration_test.py index 07bae38e9..ddebe7d80 100644 --- a/python/flink_agents/e2e_tests/e2e_tests_integration/chat_model_integration_test.py +++ b/python/flink_agents/e2e_tests/e2e_tests_integration/chat_model_integration_test.py @@ -29,17 +29,12 @@ current_dir = Path(__file__).parent TONGYI_MODEL = os.environ.get("TONGYI_CHAT_MODEL", "qwen-plus") -os.environ["TONGYI_CHAT_MODEL"] = TONGYI_MODEL OLLAMA_MODEL = os.environ.get("OLLAMA_CHAT_MODEL", "qwen3:1.7b") -os.environ["OLLAMA_CHAT_MODEL"] = OLLAMA_MODEL OPENAI_MODEL = os.environ.get("OPENAI_CHAT_MODEL", "gpt-3.5-turbo") -os.environ["OPENAI_CHAT_MODEL"] = OPENAI_MODEL AZURE_OPENAI_MODEL = os.environ.get("AZURE_OPENAI_CHAT_MODEL", "gpt-5") -os.environ["AZURE_OPENAI_CHAT_MODEL"] = AZURE_OPENAI_MODEL AZURE_OPENAI_API_VERSION = os.environ.get( "AZURE_OPENAI_API_VERSION", "2025-04-01-preview" ) -os.environ["AZURE_OPENAI_API_VERSION"] = AZURE_OPENAI_API_VERSION DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY") OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") @@ -78,8 +73,15 @@ ), ], ) -def test_chat_model_integration(model_provider: str) -> None: - os.environ["MODEL_PROVIDER"] = model_provider +def test_chat_model_integration( + model_provider: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("TONGYI_CHAT_MODEL", TONGYI_MODEL) + monkeypatch.setenv("OLLAMA_CHAT_MODEL", OLLAMA_MODEL) + monkeypatch.setenv("OPENAI_CHAT_MODEL", OPENAI_MODEL) + monkeypatch.setenv("AZURE_OPENAI_CHAT_MODEL", AZURE_OPENAI_MODEL) + monkeypatch.setenv("AZURE_OPENAI_API_VERSION", AZURE_OPENAI_API_VERSION) + monkeypatch.setenv("MODEL_PROVIDER", model_provider) env = AgentsExecutionEnvironment.get_execution_environment() input_list = [] agent = ChatModelTestAgent() diff --git a/python/flink_agents/e2e_tests/e2e_tests_integration/react_agent_test.py b/python/flink_agents/e2e_tests/e2e_tests_integration/react_agent_test.py index 85cd42bfd..04d0be41d 100644 --- a/python/flink_agents/e2e_tests/e2e_tests_integration/react_agent_test.py +++ b/python/flink_agents/e2e_tests/e2e_tests_integration/react_agent_test.py @@ -51,7 +51,6 @@ os.environ["PYTHONPATH"] = sysconfig.get_paths()["purelib"] OLLAMA_MODEL = os.environ.get("REACT_OLLAMA_MODEL", "qwen3:1.7b") -os.environ["OLLAMA_CHAT_MODEL"] = OLLAMA_MODEL class InputData(BaseModel): @@ -78,7 +77,8 @@ def get_key(self, value: Row) -> int: @pytest.mark.skipif( client is None, reason="Ollama client is not available or test model is missing" ) -def test_react_agent_on_local_runner() -> None: +def test_react_agent_on_local_runner(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("OLLAMA_CHAT_MODEL", OLLAMA_MODEL) env = AgentsExecutionEnvironment.get_execution_environment() env.get_config().set( AgentExecutionOptions.ERROR_HANDLING_STRATEGY, ErrorHandlingStrategy.RETRY @@ -138,7 +138,10 @@ def test_react_agent_on_local_runner() -> None: @pytest.mark.skipif( client is None, reason="Ollama client is not available or test model is missing" ) -def test_react_agent_on_remote_runner(tmp_path: Path) -> None: +def test_react_agent_on_remote_runner( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OLLAMA_CHAT_MODEL", OLLAMA_MODEL) stream_env = StreamExecutionEnvironment.get_execution_environment() stream_env.set_parallelism(1) diff --git a/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/chat_model_cross_language_test.py b/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/chat_model_cross_language_test.py index 42a5dd719..c17110954 100644 --- a/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/chat_model_cross_language_test.py +++ b/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/chat_model_cross_language_test.py @@ -41,7 +41,6 @@ current_dir = Path(__file__).parent OLLAMA_MODEL = os.environ.get("OLLAMA_CHAT_MODEL", "qwen3:1.7b") -os.environ["OLLAMA_CHAT_MODEL"] = OLLAMA_MODEL client = pull_model(OLLAMA_MODEL) @@ -51,7 +50,10 @@ @pytest.mark.skipif( client is None, reason="Ollama client is not available or test model is missing." ) -def test_java_chat_model_integration(tmp_path: Path) -> None: +def test_java_chat_model_integration( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OLLAMA_CHAT_MODEL", OLLAMA_MODEL) env = StreamExecutionEnvironment.get_execution_environment() env.set_runtime_mode(RuntimeExecutionMode.STREAMING) env.set_parallelism(1) diff --git a/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/yaml_cross_language_test.py b/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/yaml_cross_language_test.py index ae9920f4e..442c729eb 100644 --- a/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/yaml_cross_language_test.py +++ b/python/flink_agents/e2e_tests/e2e_tests_resource_cross_language/yaml_cross_language_test.py @@ -69,7 +69,6 @@ os.environ["PYTHONPATH"] = sysconfig.get_paths()["purelib"] OLLAMA_MODEL = os.environ.get("OLLAMA_CHAT_MODEL", "qwen3:1.7b") -os.environ["OLLAMA_CHAT_MODEL"] = OLLAMA_MODEL _client = pull_model(OLLAMA_MODEL) @@ -86,13 +85,16 @@ "flink-agents-end-to-end-tests-resource-cross-language' first." ), ) -def test_yaml_cross_language_agent(tmp_path: Path) -> None: +def test_yaml_cross_language_agent( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: """``load_yaml`` → ``apply(by name)`` with a YAML-declared Java tool. Exercises a Python Ollama chat model that calls a Java ``calculateBMI`` tool declared in YAML and resolved against the cross-language test JAR. """ + monkeypatch.setenv("OLLAMA_CHAT_MODEL", OLLAMA_MODEL) config = Configuration() config.set_string("python.pythonpath", sysconfig.get_paths()["purelib"]) env = StreamExecutionEnvironment.get_execution_environment(config)