From 078534c2abd5c94add4c7db2b179bdc09d435504 Mon Sep 17 00:00:00 2001 From: Xingyao Wang Date: Fri, 20 Jun 2025 14:36:31 -0400 Subject: [PATCH] Fix httpx deprecation warning during LLM API calls (#9261) Co-authored-by: Rohit Malhotra Co-authored-by: openhands --- openhands/cli/suppress_warnings.py | 7 ------- openhands/llm/llm.py | 15 ++++++++++++++- tests/unit/test_cli_suppress_warnings.py | 20 -------------------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/openhands/cli/suppress_warnings.py b/openhands/cli/suppress_warnings.py index 1bab74d745..cad360d22a 100644 --- a/openhands/cli/suppress_warnings.py +++ b/openhands/cli/suppress_warnings.py @@ -27,13 +27,6 @@ def suppress_cli_warnings(): category=UserWarning, ) - # Suppress httpx deprecation warnings about content parameter - warnings.filterwarnings( - 'ignore', - message=".*Use 'content=<...>' to upload raw bytes/text content.*", - category=DeprecationWarning, - ) - # Suppress general deprecation warnings from dependencies during CLI usage # This catches the "Call to deprecated method get_events" warning warnings.filterwarnings( diff --git a/openhands/llm/llm.py b/openhands/llm/llm.py index 7738e19134..dcfeecd9c6 100644 --- a/openhands/llm/llm.py +++ b/openhands/llm/llm.py @@ -288,7 +288,20 @@ class LLM(RetryMixin, DebugMixin): # Record start time for latency measurement start_time = time.time() # we don't support streaming here, thus we get a ModelResponse - resp: ModelResponse = self._completion_unwrapped(*args, **kwargs) + + # Suppress httpx deprecation warnings during LiteLLM calls + # This prevents the "Use 'content=<...>' to upload raw bytes/text content" warning + # that appears when LiteLLM makes HTTP requests to LLM providers + with warnings.catch_warnings(): + warnings.filterwarnings( + 'ignore', category=DeprecationWarning, module='httpx.*' + ) + warnings.filterwarnings( + 'ignore', + message=r'.*content=.*upload.*', + category=DeprecationWarning, + ) + resp: ModelResponse = self._completion_unwrapped(*args, **kwargs) # Calculate and record latency latency = time.time() - start_time diff --git a/tests/unit/test_cli_suppress_warnings.py b/tests/unit/test_cli_suppress_warnings.py index 6bcb3d4efd..39ebc85922 100644 --- a/tests/unit/test_cli_suppress_warnings.py +++ b/tests/unit/test_cli_suppress_warnings.py @@ -29,25 +29,6 @@ class TestWarningSuppressionCLI: output = captured_output.getvalue() assert 'Pydantic serializer warnings' not in output - def test_suppress_httpx_warnings(self): - """Test that httpx deprecation warnings are suppressed.""" - # Apply suppression - suppress_cli_warnings() - - # Capture stderr to check if warnings are printed - captured_output = StringIO() - with patch('sys.stderr', captured_output): - # Trigger httpx deprecation warning - warnings.warn( - "Use 'content=<...>' to upload raw bytes/text content.\n headers, stream = encode_request(", - DeprecationWarning, - stacklevel=2, - ) - - # Should be suppressed (no output to stderr) - output = captured_output.getvalue() - assert 'content=' not in output - def test_suppress_deprecated_method_warnings(self): """Test that deprecated method warnings are suppressed.""" # Apply suppression @@ -146,5 +127,4 @@ class TestWarningSuppressionCLI: assert any( 'Pydantic serializer warnings' in str(msg) for msg in filter_messages ) - assert any('content=' in str(msg) for msg in filter_messages) assert any('deprecated method' in str(msg) for msg in filter_messages)