PolyHedra / tests /test_agent_io_template.py
yf176
fix: compact repeated options evidence in prompts
4945749
Raw History Blame Contribute Delete
3.13 kB
"""A template binds typed evidence, structured completion, and fixed rendering."""
from unittest.mock import AsyncMock
import pytest
from pydantic import BaseModel, ConfigDict, Field, ValidationError
from polyhedra.core.template_builder import AgentIOTemplate
from polyhedra.services.llm_service import LLMService
class Input(BaseModel):
model_config = ConfigDict(extra="forbid")
evidence: str
class Output(BaseModel):
model_config = ConfigDict(extra="forbid")
interpretation: str
confidence: float = Field(ge=0, le=1)
def render(output):
return f"# Trend Analysis\n\n{output.interpretation}\n\nConfidence: {output.confidence}\n"
def binding(prompt="Interpret this evidence:\n{input_json}"):
return AgentIOTemplate("TrendAnalyst", Input, Output, prompt, render)
async def test_validated_input_to_llm_to_markdown():
template = binding()
inputs = Input(evidence='Price unchanged. Literal braces: {input_json}')
service = LLMService(provider="openai", api_key="test-only")
service.complete = AsyncMock(return_value=(
'{"interpretation":"Price is unchanged.","confidence":0.5}', 12, 15,
))
result = await service.complete_structured(template.build_prompt(inputs), template.output_schema)
assert template.render_markdown(result.output) == (
"# Trend Analysis\n\nPrice is unchanged.\n\nConfidence: 0.5\n"
)
assert "Literal braces: {input_json}" in service.complete.call_args.args[0]
@pytest.mark.parametrize("prompt", ["No evidence", "{input_json}{input_json}", "{input_json.__class__}", "{files}"])
def test_prompt_cannot_request_undeclared_inputs(prompt):
with pytest.raises(ValueError):
binding(prompt)
def test_wrong_schema_cannot_be_rendered_or_used_as_input():
with pytest.raises(TypeError):
binding().build_prompt(Output(interpretation="test", confidence=0.5))
with pytest.raises(TypeError):
binding().render_markdown(Input(evidence="test"))
def test_mutated_payload_is_revalidated_before_rendering():
output = Output(interpretation="test", confidence=0.5)
output.confidence = 2
with pytest.raises(ValidationError):
binding().render_markdown(output)
def test_prompt_projection_preserves_typed_validation_and_does_not_mutate_input():
seen = []
def project(inputs):
seen.append(inputs)
return {"evidence_summary": inputs.evidence[:5], "raw_evidence_omitted": True}
template = AgentIOTemplate(
"TrendAnalyst", Input, Output, "Interpret this evidence:\n{input_json}", render,
prompt_projector=project,
)
inputs = Input(evidence="large evidence payload")
prompt = template.build_prompt(inputs)
assert seen == [inputs]
assert '"evidence_summary": "large"' in prompt
assert '"raw_evidence_omitted": true' in prompt
assert "large evidence payload" not in prompt
assert inputs.evidence == "large evidence payload"
inputs.evidence = 5
with pytest.warns(UserWarning, match="Pydantic serializer warnings"):
with pytest.raises(ValidationError):
template.build_prompt(inputs)