Skip to content

Commit cf1da90

Browse files
committed
support double instructions
1 parent ee21cfb commit cf1da90

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

pydantic_ai_slim/pydantic_ai/_cli/web.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def run_web_command( # noqa: C901
4141
port: Port to bind the server to.
4242
models: List of model strings (e.g., ['openai:gpt-5', 'anthropic:claude-sonnet-4-5']).
4343
tools: List of builtin tool IDs (e.g., ['web_search', 'code_execution']).
44-
instructions: System instructions for generic agent.
44+
instructions: System instructions. For generic agent (no agent_path), these become
45+
the agent's instructions. With an agent, these are passed as extra instructions
46+
to each run.
4547
mcp: Path to JSON file with MCP server configurations.
4648
"""
4749
console = Console()
@@ -100,11 +102,16 @@ def run_web_command( # noqa: C901
100102
console.print(f'[red]Error: {e}[/red]')
101103
return 1
102104

105+
# For generic agent, instructions are already in the agent.
106+
# For provided agent, pass instructions as extra instructions for each run.
107+
run_instructions = instructions if agent_path else None
108+
103109
app = create_web_app(
104110
agent,
105111
models=models,
106112
builtin_tools=all_tool_instances or None,
107113
toolsets=mcp_servers or None,
114+
instructions=run_instructions,
108115
)
109116

110117
agent_desc = agent_path if agent_path else 'generic agent'

pydantic_ai_slim/pydantic_ai/builtin_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def _tool_discriminator(tool_data: dict[str, Any] | AbstractBuiltinTool) -> str:
424424
return tool_data.kind
425425

426426

427-
# This function exists primarily as a clean default_factory for ModelProfile.supported_builtin_tools
427+
# used in a couple places
428428
def get_builtin_tool_types() -> frozenset[type[AbstractBuiltinTool]]:
429429
"""Get the set of all builtin tool types (excluding deprecated tools)."""
430430
return frozenset(cls for kind, cls in BUILTIN_TOOL_TYPES.items() if kind not in DEPRECATED_BUILTIN_TOOL_TYPES)

pydantic_ai_slim/pydantic_ai/models/function.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def system(self) -> str:
204204
@classmethod
205205
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
206206
"""FunctionModel supports all builtin tools for testing flexibility."""
207-
from ..builtin_tools import BUILTIN_TOOL_TYPES, DEPRECATED_BUILTIN_TOOL_TYPES
207+
from ..builtin_tools import get_builtin_tool_types
208208

209-
return frozenset(cls for kind, cls in BUILTIN_TOOL_TYPES.items() if kind not in DEPRECATED_BUILTIN_TOOL_TYPES)
209+
return get_builtin_tool_types()
210210

211211

212212
@dataclass(frozen=True, kw_only=True)

tests/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,27 @@ def test_run_web_command_generic_agent_with_instructions(mocker: MockerFixture,
555555
assert 'You are a helpful assistant' in agent._instructions
556556

557557

558+
def test_run_web_command_agent_with_instructions(
559+
mocker: MockerFixture, create_test_module: Callable[..., None], capfd: CaptureFixture[str]
560+
):
561+
"""Test run_web_command passes instructions to create_web_app when agent is provided."""
562+
563+
mock_uvicorn_run = mocker.patch('uvicorn.run')
564+
mock_create_app = mocker.patch('pydantic_ai._cli.web.create_web_app')
565+
566+
test_agent = Agent(TestModel(custom_output_text='test'))
567+
create_test_module(custom_agent=test_agent)
568+
569+
result = run_web_command(agent_path='test_module:custom_agent', instructions='Always respond in Spanish')
570+
571+
assert result == 0
572+
mock_uvicorn_run.assert_called_once()
573+
574+
# Verify instructions were passed to create_web_app
575+
call_kwargs = mock_create_app.call_args.kwargs
576+
assert call_kwargs['instructions'] == 'Always respond in Spanish'
577+
578+
558579
def test_run_web_command_agent_load_failure(capfd: CaptureFixture[str]):
559580
"""Test run_web_command returns error when agent path is invalid."""
560581

0 commit comments

Comments
 (0)