Skip to main content

Synchronous Connection Template

The synchronous connection template provides a straightforward way to connect your agent to Snowglobe for testing. This template is ideal when your application uses standard synchronous API calls and doesn’t require complex async handling.

When to Use

Use the synchronous template when:
  • Your agent uses synchronous API calls (like standard OpenAI client)
  • You don’t need complex async operations
  • You want a simple, straightforward implementation
  • Your application has moderate performance requirements

Template Code

When you run snowglobe init and select the synchronous template, Snowglobe generates this code:

Code Walkthrough

1. Imports and Setup

  • CompletionRequest: Contains the messages and context from Snowglobe test scenarios
  • CompletionFunctionOutputs: The response format expected by Snowglobe
  • OpenAI client: Standard synchronous OpenAI client for making API calls
  • Environment variable: Safely loads your OpenAI API key from environment

2. Main Function

The completion function is the entry point that Snowglobe calls for each test scenario. It must:
  • Accept a CompletionRequest parameter
  • Return a CompletionFunctionOutputs object
  • Be named exactly completion (synchronous) or acompletion (asynchronous)

3. Message Processing

The to_openai_messages() method converts Snowglobe’s message format to OpenAI’s expected format. You can:
  • Add a system prompt to guide your agent’s behavior
  • Access individual messages with request.messages
  • Extract conversation metadata with request.get_conversation_id()

4. API Call

This makes a standard synchronous call to OpenAI’s API. You can:
  • Choose any OpenAI model that fits your needs
  • Add additional parameters like temperature, max_tokens, etc.
  • Replace with your preferred LLM provider

5. Response Formatting

Snowglobe expects responses in a specific format. The CompletionFunctionOutputs object wraps your agent’s response text.

Customization Examples

Adding Custom System Prompts

Using Different LLM Providers

Error Handling

Testing Your Implementation

  1. Test the connection:
  2. Create a simulation: In your Snowglobe dashboard (or with snowglobe simulate), then copy its simulation ID.
  3. Connect to the simulation:
    As of client 1.0.0, connect replaces the previous start command and --simulation-id is required.

Performance Considerations

The synchronous template:
  • ✅ Simple and straightforward to implement
  • ✅ Good for moderate traffic scenarios
  • ✅ Easy to debug and troubleshoot
  • ⚠️ May have higher latency under heavy load
  • ⚠️ Limited concurrent request handling
For high-performance applications, consider the asynchronous connection template instead.

Next Steps