|
| 1 | +import re |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | +from pytest_mock import MockerFixture |
| 6 | + |
| 7 | +from pydantic_ai.exceptions import UserError |
| 8 | +from pydantic_ai.profiles._json_schema import InlineDefsJsonSchemaTransformer |
| 9 | +from pydantic_ai.profiles.amazon import amazon_model_profile |
| 10 | +from pydantic_ai.profiles.anthropic import anthropic_model_profile |
| 11 | +from pydantic_ai.profiles.cohere import cohere_model_profile |
| 12 | +from pydantic_ai.profiles.deepseek import deepseek_model_profile |
| 13 | +from pydantic_ai.profiles.google import GoogleJsonSchemaTransformer, google_model_profile |
| 14 | +from pydantic_ai.profiles.grok import grok_model_profile |
| 15 | +from pydantic_ai.profiles.mistral import mistral_model_profile |
| 16 | +from pydantic_ai.profiles.openai import OpenAIJsonSchemaTransformer, openai_model_profile |
| 17 | + |
| 18 | +from ..conftest import TestEnv, try_import |
| 19 | + |
| 20 | +with try_import() as imports_successful: |
| 21 | + import openai |
| 22 | + |
| 23 | + from pydantic_ai.providers.vercel import VercelProvider |
| 24 | + |
| 25 | + |
| 26 | +pytestmark = [ |
| 27 | + pytest.mark.skipif(not imports_successful(), reason='openai not installed'), |
| 28 | + pytest.mark.vcr, |
| 29 | + pytest.mark.anyio, |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +def test_vercel_provider(): |
| 34 | + provider = VercelProvider(api_key='api-key') |
| 35 | + assert provider.name == 'vercel' |
| 36 | + assert provider.base_url == 'https://ai-gateway.vercel.sh/v1' |
| 37 | + assert isinstance(provider.client, openai.AsyncOpenAI) |
| 38 | + assert provider.client.api_key == 'api-key' |
| 39 | + |
| 40 | + |
| 41 | +def test_vercel_provider_need_api_key(env: TestEnv) -> None: |
| 42 | + env.remove('VERCEL_AI_GATEWAY_API_KEY') |
| 43 | + env.remove('VERCEL_OIDC_TOKEN') |
| 44 | + with pytest.raises( |
| 45 | + UserError, |
| 46 | + match=re.escape( |
| 47 | + 'Set the `VERCEL_AI_GATEWAY_API_KEY` or `VERCEL_OIDC_TOKEN` environment variable ' |
| 48 | + 'or pass the API key via `VercelProvider(api_key=...)` to use the Vercel provider.' |
| 49 | + ), |
| 50 | + ): |
| 51 | + VercelProvider() |
| 52 | + |
| 53 | + |
| 54 | +def test_vercel_pass_openai_client() -> None: |
| 55 | + openai_client = openai.AsyncOpenAI(api_key='api-key') |
| 56 | + provider = VercelProvider(openai_client=openai_client) |
| 57 | + assert provider.client == openai_client |
| 58 | + |
| 59 | + |
| 60 | +def test_vercel_provider_model_profile(mocker: MockerFixture): |
| 61 | + provider = VercelProvider(api_key='api-key') |
| 62 | + |
| 63 | + ns = 'pydantic_ai.providers.vercel' |
| 64 | + |
| 65 | + # Mock all profile functions |
| 66 | + anthropic_mock = mocker.patch(f'{ns}.anthropic_model_profile', wraps=anthropic_model_profile) |
| 67 | + amazon_mock = mocker.patch(f'{ns}.amazon_model_profile', wraps=amazon_model_profile) |
| 68 | + cohere_mock = mocker.patch(f'{ns}.cohere_model_profile', wraps=cohere_model_profile) |
| 69 | + deepseek_mock = mocker.patch(f'{ns}.deepseek_model_profile', wraps=deepseek_model_profile) |
| 70 | + google_mock = mocker.patch(f'{ns}.google_model_profile', wraps=google_model_profile) |
| 71 | + grok_mock = mocker.patch(f'{ns}.grok_model_profile', wraps=grok_model_profile) |
| 72 | + mistral_mock = mocker.patch(f'{ns}.mistral_model_profile', wraps=mistral_model_profile) |
| 73 | + openai_mock = mocker.patch(f'{ns}.openai_model_profile', wraps=openai_model_profile) |
| 74 | + |
| 75 | + # Test openai provider |
| 76 | + profile = provider.model_profile('openai/gpt-4o') |
| 77 | + openai_mock.assert_called_with('gpt-4o') |
| 78 | + assert profile is not None |
| 79 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 80 | + |
| 81 | + # Test anthropic provider |
| 82 | + profile = provider.model_profile('anthropic/claude-3-sonnet') |
| 83 | + anthropic_mock.assert_called_with('claude-3-sonnet') |
| 84 | + assert profile is not None |
| 85 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 86 | + |
| 87 | + # Test bedrock provider |
| 88 | + profile = provider.model_profile('bedrock/anthropic.claude-3-sonnet') |
| 89 | + amazon_mock.assert_called_with('anthropic.claude-3-sonnet') |
| 90 | + assert profile is not None |
| 91 | + assert profile.json_schema_transformer == InlineDefsJsonSchemaTransformer |
| 92 | + |
| 93 | + # Test cohere provider |
| 94 | + profile = provider.model_profile('cohere/command-r-plus') |
| 95 | + cohere_mock.assert_called_with('command-r-plus') |
| 96 | + assert profile is not None |
| 97 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 98 | + |
| 99 | + # Test deepseek provider |
| 100 | + profile = provider.model_profile('deepseek/deepseek-chat') |
| 101 | + deepseek_mock.assert_called_with('deepseek-chat') |
| 102 | + assert profile is not None |
| 103 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 104 | + |
| 105 | + # Test mistral provider |
| 106 | + profile = provider.model_profile('mistral/mistral-large') |
| 107 | + mistral_mock.assert_called_with('mistral-large') |
| 108 | + assert profile is not None |
| 109 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 110 | + |
| 111 | + # Test vertex provider |
| 112 | + profile = provider.model_profile('vertex/gemini-1.5-pro') |
| 113 | + google_mock.assert_called_with('gemini-1.5-pro') |
| 114 | + assert profile is not None |
| 115 | + assert profile.json_schema_transformer == GoogleJsonSchemaTransformer |
| 116 | + |
| 117 | + # Test xai provider |
| 118 | + profile = provider.model_profile('xai/grok-beta') |
| 119 | + grok_mock.assert_called_with('grok-beta') |
| 120 | + assert profile is not None |
| 121 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
| 122 | + |
| 123 | + |
| 124 | +def test_vercel_with_http_client(): |
| 125 | + http_client = httpx.AsyncClient() |
| 126 | + provider = VercelProvider(api_key='test-key', http_client=http_client) |
| 127 | + assert provider.client.api_key == 'test-key' |
| 128 | + assert str(provider.client.base_url) == 'https://ai-gateway.vercel.sh/v1/' |
| 129 | + |
| 130 | + |
| 131 | +def test_vercel_provider_invalid_model_name(): |
| 132 | + provider = VercelProvider(api_key='api-key') |
| 133 | + |
| 134 | + with pytest.raises(UserError, match="Model name must be in 'provider/model' format"): |
| 135 | + provider.model_profile('invalid-model-name') |
| 136 | + |
| 137 | + |
| 138 | +def test_vercel_provider_unknown_provider(): |
| 139 | + provider = VercelProvider(api_key='api-key') |
| 140 | + |
| 141 | + profile = provider.model_profile('unknown/gpt-4') |
| 142 | + assert profile is not None |
| 143 | + assert profile.json_schema_transformer == OpenAIJsonSchemaTransformer |
0 commit comments