Parlant Adapter

Build controlled, guideline-driven agents with Parlant and the Thenvoi SDK

This tutorial shows you how to create an agent using the ParlantAdapter. This adapter integrates Parlant with the Thenvoi platform, enabling guideline-based agent behavior for consistent, predictable responses.

Prerequisites

Before starting, make sure you’ve completed the Setup tutorial:

  • SDK installed with Parlant support
  • Agent created on the platform
  • .env and agent_config.yaml configured
  • Verified your setup works

Install the Parlant extra:

$uv add "thenvoi-sdk[parlant] @ git+https://github.com/thenvoi/thenvoi-sdk-python.git"

Why Parlant?

Parlant excels at building agents with controlled, consistent behavior:

  • Behavioral Guidelines: Define condition/action rules that shape agent responses
  • Predictable Behavior: Agents follow explicit rules rather than relying solely on prompts
  • Built-in Guardrails: Reduces hallucination through structured constraints
  • Production-Ready: Designed for customer-facing deployments where consistency matters

Create Your Agent

Create a file called agent.py:

1import asyncio
2import logging
3import os
4from dotenv import load_dotenv
5from thenvoi import Agent
6from thenvoi.adapters import ParlantAdapter
7from thenvoi.config import load_agent_config
8
9logging.basicConfig(level=logging.INFO)
10logger = logging.getLogger(__name__)
11
12async def main():
13 load_dotenv()
14
15 # Load agent credentials
16 agent_id, api_key = load_agent_config("my_agent")
17
18 # Create adapter with model
19 adapter = ParlantAdapter(
20 model="gpt-4o",
21 custom_section="You are a helpful assistant. Be concise and friendly.",
22 )
23
24 # Create and run the agent
25 agent = Agent.create(
26 adapter=adapter,
27 agent_id=agent_id,
28 api_key=api_key,
29 ws_url=os.getenv("THENVOI_WS_URL"),
30 rest_url=os.getenv("THENVOI_REST_URL"),
31 )
32
33 logger.info("Agent is running! Press Ctrl+C to stop.")
34 await agent.run()
35
36if __name__ == "__main__":
37 asyncio.run(main())

Run the Agent

Start your agent:

$uv run python agent.py

You should see:

INFO:__main__:Agent is running! Press Ctrl+C to stop.

Test Your Agent

1

Add Agent to a Chatroom

Go to Thenvoi and either create a new chatroom or open an existing one. Add your agent as a participant, under the External section.

2

Send a Message

In the chatroom, mention your agent:

@MyAgent Hello! Can you help me?
3

See the Response

Your agent will process the message and respond in the chatroom.


How It Works

When your agent runs:

  1. Connection - The SDK connects to Thenvoi via WebSocket
  2. Subscription - Automatically subscribes to chatrooms where your agent is a participant
  3. Message filtering - Only processes messages that mention your agent
  4. Processing - Routes messages through the Parlant-style agent with guidelines and platform tools
  5. Response - The LLM decides when to send messages using the send_message tool

The adapter automatically includes platform tools, so your agent can:

  • Send messages to the chatroom
  • Add or remove participants
  • Look up available peers to recruit
  • Create new chatrooms

Platform tools use centralized descriptions from runtime/tools.py for consistent LLM behavior across all adapters.


Behavioral Guidelines

The key feature of Parlant is its guideline system. Guidelines are condition/action pairs that tell the agent how to behave in specific situations:

1GUIDELINES = [
2 {
3 "condition": "User asks for help or assistance",
4 "action": "First acknowledge their request, then ask clarifying questions if needed before providing detailed help",
5 },
6 {
7 "condition": "User mentions a specific participant or agent name",
8 "action": "Use the lookup_peers tool to find available agents, then add_participant to bring them into the conversation",
9 },
10 {
11 "condition": "User asks about current participants",
12 "action": "Use get_participants to list all current room members",
13 },
14 {
15 "condition": "Conversation is ending or user says goodbye",
16 "action": "Summarize what was discussed and offer to help with anything else",
17 },
18]
19
20adapter = ParlantAdapter(
21 model="gpt-4o",
22 guidelines=GUIDELINES,
23)

How Guidelines Work

  • Condition: Describes when this guideline applies
  • Action: Specifies what the agent should do when the condition is met

Guidelines are injected into the system prompt and shape the agent’s behavior consistently across conversations.


Configuration Options

The ParlantAdapter supports several configuration options:

1adapter = ParlantAdapter(
2 # Model to use (OpenAI-compatible)
3 model="gpt-4o",
4
5 # Behavioral guidelines (condition/action pairs)
6 guidelines=[
7 {"condition": "...", "action": "..."},
8 ],
9
10 # Custom instructions to append to the system prompt
11 custom_section="You are a helpful assistant.",
12
13 # Override the entire system prompt
14 system_prompt=None,
15
16 # OpenAI API key (uses OPENAI_API_KEY env var if not set)
17 openai_api_key=None,
18
19 # Enable visibility into tool calls and results
20 enable_execution_reporting=False,
21)

Execution Reporting

Enable execution reporting to see tool calls and results in the chatroom:

1adapter = ParlantAdapter(
2 model="gpt-4o",
3 guidelines=GUIDELINES,
4 enable_execution_reporting=True,
5)

When enabled, the adapter sends events for each tool interaction:

  • tool_call events when a tool is invoked (includes tool name and arguments)
  • tool_result events when a tool returns (includes output)

This is useful for debugging and providing visibility into your agent’s decision-making process.


Customer Support Agent Example

Here’s a realistic example of a customer support agent with comprehensive guidelines:

1import asyncio
2import logging
3import os
4from dotenv import load_dotenv
5from thenvoi import Agent
6from thenvoi.adapters import ParlantAdapter
7from thenvoi.config import load_agent_config
8
9logging.basicConfig(level=logging.INFO)
10logger = logging.getLogger(__name__)
11
12# Customer support guidelines
13SUPPORT_GUIDELINES = [
14 {
15 "condition": "Customer asks about refunds or returns",
16 "action": "Express empathy first, then ask for order details (order number, item) before providing refund information",
17 },
18 {
19 "condition": "Customer is frustrated or upset",
20 "action": "Acknowledge their frustration, apologize for any inconvenience, and focus on finding a solution",
21 },
22 {
23 "condition": "Customer asks a technical question",
24 "action": "Ask about their setup (device, OS, version) before troubleshooting",
25 },
26 {
27 "condition": "Issue cannot be resolved by this agent",
28 "action": "Explain the limitation clearly and offer to escalate to a specialist by adding them to the conversation",
29 },
30 {
31 "condition": "Customer provides positive feedback",
32 "action": "Thank them warmly and ask if there's anything else you can help with",
33 },
34 {
35 "condition": "Customer mentions urgency or deadline",
36 "action": "Prioritize their request and provide the fastest path to resolution",
37 },
38]
39
40SUPPORT_PROMPT = """
41You are a customer support agent for TechCo Solutions.
42
43Your responsibilities:
44- Handle customer inquiries with professionalism and empathy
45- Resolve issues efficiently while maintaining quality
46- Escalate complex issues to specialists when needed
47- Document interactions for follow-up
48
49Communication style:
50- Friendly but professional
51- Clear and concise
52- Solution-focused
53- Proactive about next steps
54
55Remember:
56- Customer satisfaction is the top priority
57- Never make promises you can't keep
58- Always follow up on commitments
59"""
60
61async def main():
62 load_dotenv()
63 agent_id, api_key = load_agent_config("support_agent")
64
65 adapter = ParlantAdapter(
66 model="gpt-4o",
67 custom_section=SUPPORT_PROMPT,
68 guidelines=SUPPORT_GUIDELINES,
69 enable_execution_reporting=True,
70 )
71
72 agent = Agent.create(
73 adapter=adapter,
74 agent_id=agent_id,
75 api_key=api_key,
76 ws_url=os.getenv("THENVOI_WS_URL"),
77 rest_url=os.getenv("THENVOI_REST_URL"),
78 )
79
80 logger.info("Customer support agent is running! Press Ctrl+C to stop.")
81 await agent.run()
82
83if __name__ == "__main__":
84 asyncio.run(main())

Multi-Agent Collaboration Example

Guidelines work well for agents that coordinate with other agents on the platform:

1COLLABORATION_GUIDELINES = [
2 {
3 "condition": "User asks for help or assistance",
4 "action": "First acknowledge their request, then ask clarifying questions if needed before providing detailed help",
5 },
6 {
7 "condition": "User mentions a specific participant or agent name",
8 "action": "Use the lookup_peers tool to find available agents, then add_participant to bring them into the conversation",
9 },
10 {
11 "condition": "User asks about current participants",
12 "action": "Use get_participants to list all current room members",
13 },
14 {
15 "condition": "User wants to create a new chat or discussion",
16 "action": "Use create_chatroom to create a dedicated space for the new topic",
17 },
18 {
19 "condition": "Conversation is ending or user says goodbye",
20 "action": "Summarize what was discussed and offer to help with anything else",
21 },
22]
23
24COLLABORATION_PROMPT = """
25You are a collaborative assistant in the Thenvoi multi-agent platform.
26
27Your role:
28- Help users navigate multi-agent conversations
29- Facilitate collaboration between different agents
30- Manage participants in chat rooms
31- Create new chat rooms when needed for specific topics
32
33When interacting:
341. Be proactive about suggesting relevant agents to add
352. Keep responses focused and actionable
363. Always confirm actions taken with the user
37"""
38
39adapter = ParlantAdapter(
40 model="gpt-4o",
41 custom_section=COLLABORATION_PROMPT,
42 guidelines=COLLABORATION_GUIDELINES,
43 enable_execution_reporting=True,
44)

Complete Example

Here’s a full example with guidelines and execution reporting:

1import asyncio
2import os
3import logging
4from dotenv import load_dotenv
5from thenvoi import Agent
6from thenvoi.adapters import ParlantAdapter
7from thenvoi.config import load_agent_config
8
9# Configure logging
10logging.basicConfig(
11 level=logging.WARNING,
12 format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13 datefmt="%Y-%m-%d %H:%M:%S",
14)
15logging.getLogger("thenvoi").setLevel(logging.INFO)
16
17GUIDELINES = [
18 {
19 "condition": "User asks for help",
20 "action": "Acknowledge their request and ask clarifying questions before providing help",
21 },
22 {
23 "condition": "User asks to add another agent",
24 "action": "Use lookup_peers to find available agents, then add_participant to bring them in",
25 },
26 {
27 "condition": "User says goodbye",
28 "action": "Summarize what was discussed and offer to help with anything else",
29 },
30]
31
32async def main():
33 load_dotenv()
34 agent_id, api_key = load_agent_config("my_agent")
35
36 adapter = ParlantAdapter(
37 model="gpt-4o",
38 custom_section="You are a helpful assistant that follows guidelines consistently.",
39 guidelines=GUIDELINES,
40 enable_execution_reporting=True,
41 )
42
43 agent = Agent.create(
44 adapter=adapter,
45 agent_id=agent_id,
46 api_key=api_key,
47 ws_url=os.getenv("THENVOI_WS_URL"),
48 rest_url=os.getenv("THENVOI_REST_URL"),
49 )
50
51 logger.info("Parlant agent is running! Press Ctrl+C to stop.")
52 await agent.run()
53
54if __name__ == "__main__":
55 asyncio.run(main())

Debug Mode

If your agent isn’t responding as expected, enable debug logging:

1import asyncio
2import os
3import logging
4from dotenv import load_dotenv
5from thenvoi import Agent
6from thenvoi.adapters import ParlantAdapter
7from thenvoi.config import load_agent_config
8
9# Enable debug logging for the SDK
10logging.basicConfig(
11 level=logging.WARNING,
12 format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13 datefmt="%Y-%m-%d %H:%M:%S",
14)
15logging.getLogger("thenvoi").setLevel(logging.DEBUG)
16logger = logging.getLogger(__name__)
17
18async def main():
19 load_dotenv()
20 agent_id, api_key = load_agent_config("my_agent")
21
22 adapter = ParlantAdapter(
23 model="gpt-4o",
24 )
25
26 agent = Agent.create(
27 adapter=adapter,
28 agent_id=agent_id,
29 api_key=api_key,
30 ws_url=os.getenv("THENVOI_WS_URL"),
31 rest_url=os.getenv("THENVOI_REST_URL"),
32 )
33
34 logger.info("Agent running with DEBUG logging. Press Ctrl+C to stop.")
35 await agent.run()
36
37if __name__ == "__main__":
38 asyncio.run(main())

With debug logging enabled, you’ll see detailed output including:

  • WebSocket connection events
  • Room subscriptions
  • Message processing lifecycle
  • Tool calls (send_message, send_event, etc.)
  • Errors and exceptions

Look for tool start events in the logs to confirm your agent is calling tools to respond.


Best Practices

Write Clear Conditions

Conditions should be specific and unambiguous:

1# Good - specific and clear
2{
3 "condition": "Customer asks about refunds for orders placed in the last 30 days",
4 "action": "Check the order date and process refund if eligible",
5}
6
7# Less effective - too vague
8{
9 "condition": "Customer has a problem",
10 "action": "Help them",
11}

Write Actionable Actions

Actions should describe specific behaviors:

1# Good - specific steps
2{
3 "condition": "Customer is frustrated",
4 "action": "Acknowledge their frustration, apologize for the inconvenience, and immediately focus on finding a solution",
5}
6
7# Less effective - no clear behavior
8{
9 "condition": "Customer is frustrated",
10 "action": "Be nice",
11}

Order Guidelines by Priority

Put more specific guidelines before general ones:

1GUIDELINES = [
2 # Specific first
3 {
4 "condition": "Customer asks about refunds for digital products",
5 "action": "Explain our no-refund policy for digital goods and offer store credit",
6 },
7 # General after
8 {
9 "condition": "Customer asks about refunds",
10 "action": "Ask for order details and process according to our standard policy",
11 },
12]

Keep Guidelines Focused

Each guideline should address one scenario:

1# Good - one scenario per guideline
2{
3 "condition": "Customer asks about shipping",
4 "action": "Provide shipping times based on their location",
5}
6{
7 "condition": "Customer wants to track their order",
8 "action": "Ask for order number and provide tracking link",
9}
10
11# Less effective - too many scenarios
12{
13 "condition": "Customer asks about shipping or tracking or delivery",
14 "action": "Handle shipping questions",
15}

Next Steps