chore: import upstream snapshot with attribution
Harness Compat / harness compat (push) Failing after 0s
CI / test on 3.12 (standard) (push) Has been cancelled
CI / test on 3.13 (standard) (push) Has been cancelled
CI / test on 3.14 (standard) (push) Has been cancelled
CI / test on 3.10 (all-extras) (push) Has been cancelled
CI / test on 3.11 (all-extras) (push) Has been cancelled
CI / test on 3.12 (all-extras) (push) Has been cancelled
CI / test on 3.14 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.10 (pydantic-evals) (push) Has been cancelled
CI / test on 3.11 (pydantic-evals) (push) Has been cancelled
CI / test on 3.12 (pydantic-evals) (push) Has been cancelled
CI / deploy-docs-preview (push) Has been cancelled
CI / build release artifacts (push) Has been cancelled
CI / publish to PyPI (push) Has been cancelled
CI / Send tweet (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / mypy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / test on 3.10 (standard) (push) Has been cancelled
CI / test on 3.11 (standard) (push) Has been cancelled
CI / test on 3.13 (all-extras) (push) Has been cancelled
CI / test on 3.14 (all-extras) (push) Has been cancelled
CI / test on 3.10 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.11 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.12 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-evals) (push) Has been cancelled
CI / test on 3.14 (pydantic-evals) (push) Has been cancelled
CI / test on 3.10 (lowest-versions) (push) Has been cancelled
CI / test on 3.11 (lowest-versions) (push) Has been cancelled
CI / test on 3.12 (lowest-versions) (push) Has been cancelled
CI / test on 3.13 (lowest-versions) (push) Has been cancelled
CI / test on 3.14 (lowest-versions) (push) Has been cancelled
CI / test examples on 3.11 (push) Has been cancelled
CI / test examples on 3.12 (push) Has been cancelled
CI / test examples on 3.13 (push) Has been cancelled
CI / test examples on 3.14 (push) Has been cancelled
CI / coverage (push) Has been cancelled
CI / check (push) Has been cancelled
CI / deploy-docs (push) Has been cancelled
Harness Compat / harness compat (push) Failing after 0s
CI / test on 3.12 (standard) (push) Has been cancelled
CI / test on 3.13 (standard) (push) Has been cancelled
CI / test on 3.14 (standard) (push) Has been cancelled
CI / test on 3.10 (all-extras) (push) Has been cancelled
CI / test on 3.11 (all-extras) (push) Has been cancelled
CI / test on 3.12 (all-extras) (push) Has been cancelled
CI / test on 3.14 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.10 (pydantic-evals) (push) Has been cancelled
CI / test on 3.11 (pydantic-evals) (push) Has been cancelled
CI / test on 3.12 (pydantic-evals) (push) Has been cancelled
CI / deploy-docs-preview (push) Has been cancelled
CI / build release artifacts (push) Has been cancelled
CI / publish to PyPI (push) Has been cancelled
CI / Send tweet (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / mypy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / test on 3.10 (standard) (push) Has been cancelled
CI / test on 3.11 (standard) (push) Has been cancelled
CI / test on 3.13 (all-extras) (push) Has been cancelled
CI / test on 3.14 (all-extras) (push) Has been cancelled
CI / test on 3.10 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.11 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.12 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-evals) (push) Has been cancelled
CI / test on 3.14 (pydantic-evals) (push) Has been cancelled
CI / test on 3.10 (lowest-versions) (push) Has been cancelled
CI / test on 3.11 (lowest-versions) (push) Has been cancelled
CI / test on 3.12 (lowest-versions) (push) Has been cancelled
CI / test on 3.13 (lowest-versions) (push) Has been cancelled
CI / test on 3.14 (lowest-versions) (push) Has been cancelled
CI / test examples on 3.11 (push) Has been cancelled
CI / test examples on 3.12 (push) Has been cancelled
CI / test examples on 3.13 (push) Has been cancelled
CI / test examples on 3.14 (push) Has been cancelled
CI / coverage (push) Has been cancelled
CI / check (push) Has been cancelled
CI / deploy-docs (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# `pydantic_ai.models.function`
|
||||
|
||||
A model controlled by a local function.
|
||||
|
||||
[`FunctionModel`][pydantic_ai.models.function.FunctionModel] is similar to [`TestModel`](test.md),
|
||||
but allows greater control over the model's behavior.
|
||||
|
||||
Its primary use case is for more advanced unit testing than is possible with `TestModel`.
|
||||
|
||||
Here's a minimal example:
|
||||
|
||||
```py {title="function_model_usage.py" call_name="test_my_agent" noqa="I001"}
|
||||
from pydantic_ai import Agent
|
||||
from pydantic_ai import ModelMessage, ModelResponse, TextPart
|
||||
from pydantic_ai.models.function import FunctionModel, AgentInfo
|
||||
|
||||
my_agent = Agent('openai:gpt-5.2')
|
||||
|
||||
|
||||
async def model_function(
|
||||
messages: list[ModelMessage], info: AgentInfo
|
||||
) -> ModelResponse:
|
||||
print(messages)
|
||||
"""
|
||||
[
|
||||
ModelRequest(
|
||||
parts=[
|
||||
UserPromptPart(
|
||||
content='Testing my agent...',
|
||||
timestamp=datetime.datetime(...),
|
||||
)
|
||||
],
|
||||
timestamp=datetime.datetime(...),
|
||||
run_id='...',
|
||||
conversation_id='...',
|
||||
)
|
||||
]
|
||||
"""
|
||||
print(info)
|
||||
"""
|
||||
AgentInfo(
|
||||
function_tools=[],
|
||||
allow_text_output=True,
|
||||
output_tools=[],
|
||||
model_settings=None,
|
||||
model_request_parameters=ModelRequestParameters(
|
||||
function_tools=[], native_tools=[], output_tools=[]
|
||||
),
|
||||
instructions=None,
|
||||
)
|
||||
"""
|
||||
return ModelResponse(parts=[TextPart('hello world')])
|
||||
|
||||
|
||||
async def test_my_agent():
|
||||
"""Unit test for my_agent, to be run by pytest."""
|
||||
with my_agent.override(model=FunctionModel(model_function)):
|
||||
result = await my_agent.run('Testing my agent...')
|
||||
assert result.output == 'hello world'
|
||||
```
|
||||
|
||||
See [Unit testing with `FunctionModel`](../../testing.md#unit-testing-with-functionmodel) for detailed documentation.
|
||||
|
||||
::: pydantic_ai.models.function
|
||||
Reference in New Issue
Block a user