Tool results#

A ticket search returns other customers’ names and an address. Redacting the tool result with no restore keeps them from both the model and the user.

"""Keep other people's data out of the conversation.

A support agent searches tickets, and the results name other customers. The
guard on the tool result replaces their names and addresses with placeholders
before the model reads them, and nothing restores them: neither the model nor
the user asking ever sees who those customers are.

The script prints what each party sees. The model is a stand-in that follows a
script, so only an Enkrypt key is needed: set ENKRYPTAI_API_KEY in the
environment or in .env.

    pixi run -e examples python examples/pii_tool_results.py
"""

from dotenv import load_dotenv
from pydantic_ai import Agent
from pydantic_ai.messages import ModelMessage
from pydantic_ai.messages import ModelResponse
from pydantic_ai.messages import TextPart
from pydantic_ai.messages import ToolCallPart
from pydantic_ai.messages import UserPromptPart
from pydantic_ai.models.function import AgentInfo
from pydantic_ai.models.function import FunctionModel
from pydantic_ai_harness import ToolGuardrail

from pydantic_ai_enkrypt import Enkrypt

load_dotenv()

enkrypt = Enkrypt()


def model(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
    latest = messages[-1].parts[-1]
    print(f"model saw:     {latest.content}")
    if isinstance(latest, UserPromptPart):
        print('model calls:   search_tickets("login")')
        return ModelResponse(parts=[ToolCallPart("search_tickets", {"query": "login"})])
    answer = (
        "Two recent login problems: <<PERSON:1>> was blocked from an unusual "
        "address, and <<PERSON:2>> was locked out after password resets."
    )
    print(f"model answers: {answer}")
    return ModelResponse(parts=[TextPart(answer)])


agent = Agent(
    FunctionModel(model),
    capabilities=[
        ToolGuardrail(
            result_guard=enkrypt.pii(entities=["pii", "person", "ip_address"])
        ),
    ],
)


@agent.tool_plain
def search_tickets(query: str) -> str:
    tickets = (
        "Maria Lopez could not log in from 192.168.4.20. "
        "Wei Chen was locked out after resetting his password twice."
    )
    print(f"tool returns:  {tickets}")
    return tickets


prompt = "What login problems have customers reported lately?"
print(f"user types:    {prompt}")
result = agent.run_sync(prompt)
print(f"user sees:     {result.output}")

Run it with an Enkrypt key in the environment or in .env:

pixi run -e examples python examples/pii_tool_results.py

Output from a run:

user types:    What login problems have customers reported lately?
model saw:     What login problems have customers reported lately?
model calls:   search_tickets("login")
tool returns:  Maria Lopez could not log in from 192.168.4.20. Wei Chen was locked out after resetting his password twice.
model saw:     <<PERSON:1>> could not log in from <<IP_ADDRESS:1>>. <<PERSON:2>> was locked out after resetting his password twice.
model answers: Two recent login problems: <<PERSON:1>> was blocked from an unusual address, and <<PERSON:2>> was locked out after password resets.
user sees:     Two recent login problems: <<PERSON:1>> was blocked from an unusual address, and <<PERSON:2>> was locked out after password resets.

Personal data: purposes.