Skip to content

Commit 0d2dcf0

Browse files
authored
test(pydantic_ai): Fix instructions test for pydantic-ai >=2.36.0 (#7321)
Build the agent with public `instructions`/`system_prompt` kwargs instead of poking internal `_instructions`/`_system_prompts` attributes, and account for the joiner between multiple instructions changing from a single newline to a blank line in pydantic-ai 2.36.0. [There's normalization of instructions occurring under the hood as of 2.36.0](https://github.com/pydantic/pydantic-ai/blame/6246795faf941937e1759f51b0dfc65d82a4667b/pydantic_ai_slim/pydantic_ai/agent/__init__.py#L673-L676) that we don't have a reason to interfere with within the tests, so moving the configuration of instructions and the system prompt to using the public `Agent` API. Fixes PY-2755 Fixes #7306
1 parent 9f9d17a commit 0d2dcf0

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

‎tests/integrations/pydantic_ai/test_pydantic_ai.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,15 +2228,15 @@ async def test_invoke_agent_with_instructions(
22282228
"""
22292229
from pydantic_ai import Agent
22302230

2231-
# Create agent with instructions (can be string or list)
22322231
agent = Agent(
22332232
"test",
22342233
name="test_instructions",
2234+
instructions=["Instruction 1", "Instruction 2"],
2235+
system_prompt="System prompt",
22352236
)
22362237

2237-
# Add instructions via _instructions attribute (internal API)
2238-
agent._instructions = ["Instruction 1", "Instruction 2"]
2239-
agent._system_prompts = ["System prompt"]
2238+
# pydantic-ai >=2.36.0 joins multiple instructions with a blank line, earlier versions with a single newline
2239+
instructions_separator = "\n\n" if PYDANTIC_AI_VERSION >= (2, 36) else "\n"
22402240

22412241
sentry_init(
22422242
integrations=[PydanticAIIntegration(include_prompts=include_prompts)],
@@ -2268,7 +2268,10 @@ async def test_invoke_agent_with_instructions(
22682268
]
22692269
assert json.loads(system_instructions) == [
22702270
{"type": "text", "content": "System prompt"},
2271-
{"type": "text", "content": "Instruction 1\nInstruction 2"},
2271+
{
2272+
"type": "text",
2273+
"content": f"Instruction 1{instructions_separator}Instruction 2",
2274+
},
22722275
]
22732276
else:
22742277
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in chat_span["attributes"]
@@ -2291,7 +2294,10 @@ async def test_invoke_agent_with_instructions(
22912294
system_instructions = chat_span["data"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]
22922295
assert json.loads(system_instructions) == [
22932296
{"type": "text", "content": "System prompt"},
2294-
{"type": "text", "content": "Instruction 1\nInstruction 2"},
2297+
{
2298+
"type": "text",
2299+
"content": f"Instruction 1{instructions_separator}Instruction 2",
2300+
},
22952301
]
22962302
else:
22972303
assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in chat_span["data"]

0 commit comments

Comments
 (0)