9201ef759e
CI / lint (push) Waiting to run
CI / mypy (push) Waiting to run
CI / docs (push) Waiting to run
CI / test on 3.10 (standard) (push) Waiting to run
CI / test on 3.11 (standard) (push) Waiting to run
CI / test on 3.12 (standard) (push) Waiting to run
CI / test on 3.10 (all-extras) (push) Waiting to run
CI / test on 3.11 (all-extras) (push) Waiting to run
CI / test on 3.12 (all-extras) (push) Waiting to run
CI / test on 3.13 (all-extras) (push) Waiting to run
CI / test on 3.14 (pydantic-evals) (push) Waiting to run
CI / test on 3.13 (standard) (push) Waiting to run
CI / test on 3.14 (standard) (push) Waiting to run
CI / test on 3.14 (all-extras) (push) Waiting to run
CI / test on 3.10 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.11 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.12 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.13 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.14 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.10 (pydantic-evals) (push) Waiting to run
CI / test on 3.11 (pydantic-evals) (push) Waiting to run
CI / test on 3.12 (pydantic-evals) (push) Waiting to run
CI / test on 3.13 (pydantic-evals) (push) Waiting to run
CI / test on 3.10 (lowest-versions) (push) Waiting to run
CI / test on 3.11 (lowest-versions) (push) Waiting to run
CI / test on 3.12 (lowest-versions) (push) Waiting to run
CI / test on 3.13 (lowest-versions) (push) Waiting to run
CI / test on 3.14 (lowest-versions) (push) Waiting to run
CI / test examples on 3.11 (push) Waiting to run
CI / test examples on 3.12 (push) Waiting to run
CI / test examples on 3.13 (push) Waiting to run
CI / test examples on 3.14 (push) Waiting to run
CI / coverage (push) Blocked by required conditions
CI / check (push) Blocked by required conditions
CI / deploy-docs (push) Blocked by required conditions
CI / deploy-docs-preview (push) Blocked by required conditions
CI / build release artifacts (push) Blocked by required conditions
CI / publish to PyPI (push) Blocked by required conditions
CI / Send tweet (push) Blocked by required conditions
Harness Compat / harness compat (push) Failing after 0s
157 lines
5.0 KiB
Python
157 lines
5.0 KiB
Python
from dataclasses import dataclass
|
|
from datetime import timezone
|
|
from typing import Any
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from pydantic_ai import (
|
|
BinaryContent,
|
|
ModelRequest,
|
|
ModelResponse,
|
|
SystemPromptPart,
|
|
TextPart,
|
|
UserPromptPart,
|
|
)
|
|
from pydantic_ai.agent import Agent
|
|
from pydantic_ai.exceptions import UnexpectedModelBehavior
|
|
|
|
from .._inline_snapshot import snapshot
|
|
from ..conftest import IsDatetime, IsNow, IsStr, try_import
|
|
|
|
with try_import() as imports_successful:
|
|
from mcp import CreateMessageResult
|
|
from mcp.types import TextContent
|
|
|
|
from pydantic_ai.models.mcp_sampling import MCPSamplingModel
|
|
|
|
pytestmark = pytest.mark.skipif(not imports_successful(), reason='mcp package not installed')
|
|
|
|
|
|
@dataclass
|
|
class FakeSession:
|
|
create_message: Any
|
|
|
|
|
|
def fake_session(create_message: Any) -> Any:
|
|
return FakeSession(create_message)
|
|
|
|
|
|
def test_mcp_sampling_model():
|
|
model = MCPSamplingModel(fake_session(AsyncMock()))
|
|
assert model.model_name == 'mcp-sampling'
|
|
assert model.system == 'MCP'
|
|
|
|
|
|
def test_assistant_text():
|
|
result = CreateMessageResult(
|
|
role='assistant', content=TextContent(type='text', text='text content'), model='test-model'
|
|
)
|
|
create_message = AsyncMock(return_value=result)
|
|
agent = Agent(model=MCPSamplingModel(fake_session(create_message)))
|
|
|
|
result = agent.run_sync('Hello')
|
|
assert result.output == snapshot('text content')
|
|
assert result.all_messages() == snapshot(
|
|
[
|
|
ModelRequest(
|
|
parts=[
|
|
UserPromptPart(
|
|
content='Hello',
|
|
timestamp=IsNow(tz=timezone.utc),
|
|
)
|
|
],
|
|
timestamp=IsDatetime(),
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
ModelResponse(
|
|
parts=[TextPart(content='text content')],
|
|
model_name='test-model',
|
|
timestamp=IsNow(tz=timezone.utc),
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
]
|
|
)
|
|
|
|
|
|
def test_user_text():
|
|
result = CreateMessageResult(role='user', content=TextContent(type='text', text='text content'), model='test-model')
|
|
create_message = AsyncMock(return_value=result)
|
|
agent = Agent(model=MCPSamplingModel(fake_session(create_message)))
|
|
|
|
expected_match = 'Unexpected result from MCP sampling, expected "assistant" role, got user.'
|
|
with pytest.raises(UnexpectedModelBehavior, match=expected_match):
|
|
agent.run_sync('Hello')
|
|
|
|
|
|
def test_assistant_text_history():
|
|
result = CreateMessageResult(
|
|
role='assistant', content=TextContent(type='text', text='text content'), model='test-model'
|
|
)
|
|
create_message = AsyncMock(return_value=result)
|
|
agent = Agent(model=MCPSamplingModel(fake_session(create_message)), instructions='testing')
|
|
|
|
result = agent.run_sync('1')
|
|
result = agent.run_sync('2', message_history=result.all_messages())
|
|
|
|
assert result.output == snapshot('text content')
|
|
assert result.all_messages() == snapshot(
|
|
[
|
|
ModelRequest(
|
|
parts=[UserPromptPart(content='1', timestamp=IsNow(tz=timezone.utc))],
|
|
timestamp=IsDatetime(),
|
|
instructions='testing',
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
ModelResponse(
|
|
parts=[TextPart(content='text content')],
|
|
model_name='test-model',
|
|
timestamp=IsNow(tz=timezone.utc),
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
ModelRequest(
|
|
parts=[UserPromptPart(content='2', timestamp=IsNow(tz=timezone.utc))],
|
|
timestamp=IsDatetime(),
|
|
instructions='testing',
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
ModelResponse(
|
|
parts=[TextPart(content='text content')],
|
|
model_name='test-model',
|
|
timestamp=IsNow(tz=timezone.utc),
|
|
run_id=IsStr(),
|
|
conversation_id=IsStr(),
|
|
),
|
|
]
|
|
)
|
|
|
|
|
|
def test_assistant_text_history_complex():
|
|
history = [
|
|
ModelRequest(
|
|
parts=[
|
|
UserPromptPart(content='1'),
|
|
UserPromptPart(content=['a string', BinaryContent(data=b'data', media_type='image/jpeg')]),
|
|
SystemPromptPart(content='system content'),
|
|
],
|
|
timestamp=IsDatetime(),
|
|
),
|
|
ModelResponse(
|
|
parts=[TextPart(content='text content')],
|
|
model_name='test-model',
|
|
),
|
|
]
|
|
|
|
result = CreateMessageResult(
|
|
role='assistant', content=TextContent(type='text', text='text content'), model='test-model'
|
|
)
|
|
create_message = AsyncMock(return_value=result)
|
|
agent = Agent(model=MCPSamplingModel(fake_session(create_message)))
|
|
result = agent.run_sync('1', message_history=history)
|
|
assert result.output == snapshot('text content')
|