Documentation Index
Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
Use this file to discover all available pages before exploring further.
Display Functions
The display functions provide a standardized way to format and display agent interactions, tool calls, reflections, and other outputs in PraisonAI. These utilities ensure consistent formatting across all agent interactions.
Import
from praisonaiagents import (
display_interaction,
display_self_reflection,
display_instruction,
display_tool_call,
display_error,
display_generating,
clean_triple_backticks,
register_display_callback,
sync_display_callbacks,
async_display_callbacks
)
Display Functions
display_interaction
Displays an interaction between agents or between an agent and a user.
def display_interaction(
sender: str,
receiver: str,
message: str,
sender_type: str = "agent",
receiver_type: str = "agent"
) -> None
Parameters:
sender (str): Name of the sender
receiver (str): Name of the receiver
message (str): The message content
sender_type (str, optional): Type of sender (“agent” or “user”). Defaults to “agent”
receiver_type (str, optional): Type of receiver (“agent” or “user”). Defaults to “agent”
Example:
display_interaction(
sender="ResearchAgent",
receiver="WriterAgent",
message="I've found 5 relevant sources on AI ethics.",
sender_type="agent",
receiver_type="agent"
)
display_self_reflection
Displays an agent’s self-reflection or internal thoughts.
def display_self_reflection(
agent_name: str,
reflection: str
) -> None
Parameters:
agent_name (str): Name of the agent performing self-reflection
reflection (str): The reflection content
Example:
display_self_reflection(
agent_name="AnalysisAgent",
reflection="The data suggests a trend, but I should verify with additional sources."
)
display_instruction
Displays instructions given to an agent.
def display_instruction(
agent_name: str,
instruction: str
) -> None
Parameters:
agent_name (str): Name of the agent receiving the instruction
instruction (str): The instruction content
Example:
display_instruction(
agent_name="DataAgent",
instruction="Analyze the sales data from Q4 2023 and identify key trends."
)
Displays a tool being called by an agent.
def display_tool_call(
agent_name: str,
tool_name: str,
parameters: Optional[Dict[str, Any]] = None,
result: Optional[Any] = None
) -> None
Parameters:
agent_name (str): Name of the agent calling the tool
tool_name (str): Name of the tool being called
parameters (Dict[str, Any], optional): Parameters passed to the tool
result (Any, optional): Result returned by the tool
Example:
display_tool_call(
agent_name="SearchAgent",
tool_name="web_search",
parameters={"query": "latest AI research papers"},
result="Found 10 relevant papers..."
)
display_error
Displays an error message with proper formatting.
def display_error(
error_type: str,
error_message: str,
agent_name: Optional[str] = None
) -> None
Parameters:
error_type (str): Type of error (e.g., “ValidationError”, “ConnectionError”)
error_message (str): The error message
agent_name (str, optional): Name of the agent that encountered the error
Example:
display_error(
error_type="APIError",
error_message="Rate limit exceeded. Please try again later.",
agent_name="WeatherAgent"
)
display_generating
Displays a generating/thinking indicator for long-running operations.
def display_generating(
agent_name: str,
action: str = "Thinking"
) -> None
Parameters:
agent_name (str): Name of the agent that is processing
action (str, optional): Description of what the agent is doing. Defaults to “Thinking”
Example:
display_generating(
agent_name="CodeAgent",
action="Generating code solution"
)
clean_triple_backticks
Removes triple backticks from code blocks for clean display.
def clean_triple_backticks(text: str) -> str
Parameters:
text (str): Text containing triple backticks
Returns:
str: Text with triple backticks removed
Example:
code = """```python
def hello():
print("Hello World")
```"""
clean_code = clean_triple_backticks(code)
# Returns: "def hello():\n print(\"Hello World\")"
Callback Functions
register_display_callback
Registers a custom display callback function for handling display events.
def register_display_callback(
callback: Callable,
event_types: Optional[List[str]] = None
) -> None
Parameters:
callback (Callable): The callback function to register
event_types (List[str], optional): List of event types to register for. If None, registers for all events
Event Types:
"interaction": Agent-to-agent or agent-to-user interactions
"reflection": Self-reflection events
"instruction": Instruction events
"tool_call": Tool calling events
"error": Error events
"generating": Processing/generating events
Example:
def custom_logger(event_type, data):
print(f"[{event_type}] {data}")
register_display_callback(
callback=custom_logger,
event_types=["interaction", "error"]
)
sync_display_callbacks
Context manager for synchronous display callbacks.
@contextmanager
def sync_display_callbacks() -> None
Example:
with sync_display_callbacks():
# All display events within this context will be processed synchronously
agent.run("Analyze the data")
async_display_callbacks
Context manager for asynchronous display callbacks.
@asynccontextmanager
async def async_display_callbacks() -> None
Example:
async with async_display_callbacks():
# All display events within this context will be processed asynchronously
await agent.arun("Analyze the data")
Error Logging
error_logs
Retrieves error logs for debugging and monitoring.
def error_logs(
agent_name: Optional[str] = None,
limit: Optional[int] = None,
error_type: Optional[str] = None
) -> List[Dict[str, Any]]
Parameters:
agent_name (str, optional): Filter logs by agent name
limit (int, optional): Maximum number of logs to return
error_type (str, optional): Filter logs by error type
Returns:
List[Dict[str, Any]]: List of error log entries
Example:
# Get all error logs
all_errors = error_logs()
# Get last 10 errors for a specific agent
agent_errors = error_logs(agent_name="DataAgent", limit=10)
# Get all API errors
api_errors = error_logs(error_type="APIError")
Best Practices
- Consistent Display: Use display functions instead of print() for consistent formatting
- Error Handling: Always use display_error() for errors to ensure they’re properly logged
- Custom Callbacks: Register callbacks early in your application lifecycle
- Performance: Use async callbacks for high-volume display events
- Debugging: Use error_logs() to retrieve historical error information
Example: Complete Display Flow
from praisonaiagents import (
Agent,
display_instruction,
display_generating,
display_interaction,
display_error,
register_display_callback,
error_logs
)
# Register a custom logger
def log_to_file(event_type, data):
with open("agent_logs.txt", "a") as f:
f.write(f"[{event_type}] {data}\n")
register_display_callback(log_to_file)
# Create and run agent
agent = Agent(name="AnalysisAgent", instructions="Analyze data trends")
# Display the instruction
display_instruction("AnalysisAgent", "Analyze Q4 sales data")
# Show processing
display_generating("AnalysisAgent", "Analyzing data")
try:
result = agent.run("What are the key trends in Q4?")
display_interaction("User", "AnalysisAgent", "What are the key trends in Q4?", "user", "agent")
display_interaction("AnalysisAgent", "User", result, "agent", "user")
except Exception as e:
display_error("RuntimeError", str(e), "AnalysisAgent")
# Check for errors
errors = error_logs(agent_name="AnalysisAgent")
if errors:
print(f"Found {len(errors)} errors")
See Also