-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vertexai: add retry tracing with metadata in ChatAnthropicVertex (#728)
- Loading branch information
1 parent
ef5932d
commit b341070
Showing
3 changed files
with
245 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
libs/vertexai/tests/unit_tests/test_model_garden_retry.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from anthropic import APIError | ||
|
||
from langchain_google_vertexai.model_garden import ( | ||
_create_retry_decorator, | ||
) | ||
|
||
|
||
def create_api_error(): | ||
"""Helper function to create an APIError with required arguments.""" | ||
mock_request = MagicMock() | ||
mock_request.method = "POST" | ||
mock_request.url = "test-url" | ||
mock_request.headers = {} | ||
mock_request.body = None | ||
return APIError( | ||
message="Test error", | ||
request=mock_request, | ||
body={"error": {"message": "Test error"}}, | ||
) | ||
|
||
|
||
def test_retry_on_errors(): | ||
"""Test that the retry decorator works with sync functions.""" | ||
mock_llm = MagicMock() | ||
mock_llm.max_retries = 2 | ||
mock_function = MagicMock(side_effect=[create_api_error(), "success"]) | ||
|
||
decorator = _create_retry_decorator(mock_llm) | ||
wrapped_func = decorator(mock_function) | ||
|
||
result = wrapped_func() | ||
assert result == "success" | ||
assert mock_function.call_count == 2 | ||
|
||
|
||
def test_max_retries_exceeded(): | ||
"""Test that the retry decorator fails after max retries.""" | ||
mock_llm = MagicMock() | ||
mock_llm.max_retries = 2 | ||
mock_function = MagicMock(side_effect=[create_api_error(), create_api_error()]) | ||
|
||
decorator = _create_retry_decorator(mock_llm) | ||
wrapped_func = decorator(mock_function) | ||
|
||
with pytest.raises(APIError): | ||
wrapped_func() | ||
assert mock_function.call_count == 2 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_retry_on_errors(): | ||
"""Test that the retry decorator works with async functions.""" | ||
mock_llm = MagicMock() | ||
mock_llm.max_retries = 2 | ||
|
||
class AsyncMock: | ||
def __init__(self): | ||
self.call_count = 0 | ||
|
||
async def __call__(self): | ||
self.call_count += 1 | ||
if self.call_count == 1: | ||
raise create_api_error() | ||
return "success" | ||
|
||
mock_async = AsyncMock() | ||
|
||
decorator = _create_retry_decorator(mock_llm) | ||
wrapped_func = decorator(mock_async) | ||
|
||
result = await wrapped_func() | ||
assert result == "success" | ||
assert mock_async.call_count == 2 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_max_retries_exceeded(): | ||
"""Test that the async retry decorator fails after max retries.""" | ||
mock_llm = MagicMock() | ||
mock_llm.max_retries = 2 | ||
|
||
class AsyncMock: | ||
def __init__(self): | ||
self.call_count = 0 | ||
|
||
async def __call__(self): | ||
self.call_count += 1 | ||
raise create_api_error() | ||
|
||
mock_async = AsyncMock() | ||
|
||
decorator = _create_retry_decorator(mock_llm) | ||
wrapped_func = decorator(mock_async) | ||
|
||
with pytest.raises(APIError): | ||
await wrapped_func() | ||
assert mock_async.call_count == 2 |