|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import sys
|
2 | 4 | from collections.abc import AsyncIterator
|
3 | 5 | from datetime import timezone
|
4 | 6 |
|
5 | 7 | import pytest
|
| 8 | +from dirty_equals import IsJson |
6 | 9 | from inline_snapshot import snapshot
|
7 | 10 |
|
8 | 11 | from pydantic_ai import Agent, ModelHTTPError
|
9 | 12 | from pydantic_ai.messages import ModelMessage, ModelRequest, ModelResponse, TextPart, UserPromptPart
|
10 | 13 | from pydantic_ai.models.fallback import FallbackModel
|
11 | 14 | from pydantic_ai.models.function import AgentInfo, FunctionModel
|
12 | 15 |
|
13 |
| -from ..conftest import IsNow |
| 16 | +from ..conftest import IsNow, try_import |
14 | 17 |
|
15 | 18 | if sys.version_info < (3, 11):
|
16 | 19 | from exceptiongroup import ExceptionGroup as ExceptionGroup
|
17 | 20 | else:
|
18 | 21 | ExceptionGroup = ExceptionGroup
|
19 | 22 |
|
| 23 | +with try_import() as logfire_imports_successful: |
| 24 | + from logfire.testing import CaptureLogfire |
| 25 | + |
| 26 | + |
20 | 27 | pytestmark = pytest.mark.anyio
|
21 | 28 |
|
22 | 29 |
|
@@ -86,6 +93,100 @@ def test_first_failed() -> None:
|
86 | 93 | )
|
87 | 94 |
|
88 | 95 |
|
| 96 | +@pytest.mark.skipif(not logfire_imports_successful(), reason='logfire not installed') |
| 97 | +def test_first_failed_instrumented(capfire: CaptureLogfire) -> None: |
| 98 | + fallback_model = FallbackModel(failure_model, success_model) |
| 99 | + agent = Agent(model=fallback_model, instrument=True) |
| 100 | + result = agent.run_sync('hello') |
| 101 | + assert result.data == snapshot('success') |
| 102 | + assert result.all_messages() == snapshot( |
| 103 | + [ |
| 104 | + ModelRequest( |
| 105 | + parts=[ |
| 106 | + UserPromptPart( |
| 107 | + content='hello', |
| 108 | + timestamp=IsNow(tz=timezone.utc), |
| 109 | + ) |
| 110 | + ] |
| 111 | + ), |
| 112 | + ModelResponse( |
| 113 | + parts=[TextPart(content='success')], |
| 114 | + model_name='function:success_response:', |
| 115 | + timestamp=IsNow(tz=timezone.utc), |
| 116 | + ), |
| 117 | + ] |
| 118 | + ) |
| 119 | + assert capfire.exporter.exported_spans_as_dict() == snapshot( |
| 120 | + [ |
| 121 | + { |
| 122 | + 'name': 'preparing model request params', |
| 123 | + 'context': {'trace_id': 1, 'span_id': 3, 'is_remote': False}, |
| 124 | + 'parent': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 125 | + 'start_time': 2000000000, |
| 126 | + 'end_time': 3000000000, |
| 127 | + 'attributes': { |
| 128 | + 'run_step': 1, |
| 129 | + 'logfire.span_type': 'span', |
| 130 | + 'logfire.msg': 'preparing model request params', |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + 'name': 'chat function:success_response:', |
| 135 | + 'context': {'trace_id': 1, 'span_id': 5, 'is_remote': False}, |
| 136 | + 'parent': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 137 | + 'start_time': 4000000000, |
| 138 | + 'end_time': 5000000000, |
| 139 | + 'attributes': { |
| 140 | + 'gen_ai.operation.name': 'chat', |
| 141 | + 'logfire.span_type': 'span', |
| 142 | + 'logfire.msg': 'chat FallBackModel[function:failure_response:, function:success_response:]', |
| 143 | + 'gen_ai.usage.input_tokens': 51, |
| 144 | + 'gen_ai.usage.output_tokens': 1, |
| 145 | + 'gen_ai.system': 'function', |
| 146 | + 'gen_ai.request.model': 'function:success_response:', |
| 147 | + 'gen_ai.response.model': 'function:success_response:', |
| 148 | + 'events': IsJson( |
| 149 | + [ |
| 150 | + { |
| 151 | + 'content': 'hello', |
| 152 | + 'role': 'user', |
| 153 | + 'gen_ai.system': 'function', |
| 154 | + 'gen_ai.message.index': 0, |
| 155 | + 'event.name': 'gen_ai.user.message', |
| 156 | + }, |
| 157 | + { |
| 158 | + 'index': 0, |
| 159 | + 'message': {'role': 'assistant', 'content': 'success'}, |
| 160 | + 'gen_ai.system': 'function', |
| 161 | + 'event.name': 'gen_ai.choice', |
| 162 | + }, |
| 163 | + ] |
| 164 | + ), |
| 165 | + 'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}}}', |
| 166 | + }, |
| 167 | + }, |
| 168 | + { |
| 169 | + 'name': 'agent run', |
| 170 | + 'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 171 | + 'parent': None, |
| 172 | + 'start_time': 1000000000, |
| 173 | + 'end_time': 6000000000, |
| 174 | + 'attributes': { |
| 175 | + 'model_name': 'FallBackModel[function:failure_response:, function:success_response:]', |
| 176 | + 'agent_name': 'agent', |
| 177 | + 'logfire.msg': 'agent run', |
| 178 | + 'logfire.span_type': 'span', |
| 179 | + 'gen_ai.usage.input_tokens': 51, |
| 180 | + 'gen_ai.usage.output_tokens': 1, |
| 181 | + 'all_messages_events': '[{"content": "hello", "role": "user", "gen_ai.message.index": 0, "event.name": "gen_ai.user.message"}, {"role": "assistant", "content": "success", "gen_ai.message.index": 1, "event.name": "gen_ai.assistant.message"}]', |
| 182 | + 'final_result': 'success', |
| 183 | + 'logfire.json_schema': '{"type": "object", "properties": {"all_messages_events": {"type": "array"}, "final_result": {"type": "object"}}}', |
| 184 | + }, |
| 185 | + }, |
| 186 | + ] |
| 187 | + ) |
| 188 | + |
| 189 | + |
89 | 190 | def test_all_failed() -> None:
|
90 | 191 | fallback_model = FallbackModel(failure_model, failure_model)
|
91 | 192 | agent = Agent(model=fallback_model)
|
|
0 commit comments