Skip to main content
This tutorial will guide you through building streaming clients in Python and TypeScript that connect to existing Thesis.io conversations, allowing you to continue DeFi research and ask follow-up questions in real-time.
This tutorial assumes you have an existing conversation ID from a previous Thesis.io session. If you need to create a new conversation first, check out our Quickstart guide.

Prerequisites

Before starting, make sure you have:
  • For Python: Python 3.7+ installed with async programming knowledge
  • For TypeScript: Node.js 16+ and TypeScript experience
  • A valid Thesis.io Space API key
  • An existing conversation ID from a previous session
  • Understanding of streaming and event-driven programming

Get your Thesis.io API key

Required Dependencies

Install the required Python packages:

Environment Setup

Create a .env file in your project root:
The TypeScript client defaults to http://localhost:3000 for local development. You can override this by setting the API_BASE_URL environment variable or modifying the base URL in your code.

Building the Streaming Client

Let’s break down the streaming client into digestible components for both Python and TypeScript:

1. Core Client Class

First, we’ll create the main streaming client class that handles HTTP connections:
The Python version uses async context managers for resource management, while the TypeScript version relies on Node.js’s built-in memory management for HTTP connections.

2. Event Handler System

The streaming API returns various types of events. Here’s how to handle them:

3. Streaming Connection Logic

The core streaming functionality handles chunked JSON responses. The TypeScript implementation uses Node.js’s built-in fetch API:
The JSON buffer handling is crucial because streaming responses can arrive in chunks, potentially splitting JSON objects across multiple chunks. The TypeScript version assumes one JSON object per accumulated buffer for simplicity.

4. Main Application Logic

Here’s how to tie everything together:

Complete Implementation

Here’s the complete implementation you can copy and use:

Running the Code

  1. Save the code to a file named join_conversation.py
  2. Set up your environment:
  3. Update the conversation ID: Replace '4b03707134ee42b4abf613353f746b6c' in the code with your actual conversation ID from a previous Thesis.io session.
  4. Run the client:

Understanding the Output

The streaming client will display different types of events:
  • πŸ”— Connection events: When the client connects/disconnects
  • πŸ“¨ Socket events: Main conversation data and responses
  • 🏁 Completion: When the conversation finishes or requires user input
  • ❌ Errors: Any issues during the streaming process
Both Python and TypeScript clients automatically handle JSON chunking and provide detailed logging. The TypeScript version uses native Node.js APIs for a lightweight, dependency-free approach.

TypeScript Implementation Benefits

The TypeScript implementation provides several advantages:

Native Node.js APIs

  • Built-in fetch: Uses Node.js 18+ native fetch API
  • No dependencies: No external HTTP libraries required
  • URL class: Built-in URL construction and validation
  • TextDecoder: Native streaming text decoding

Modern JavaScript Features

  • ReadableStream: Native stream processing for optimal performance
  • Promise-based: Clean async/await patterns
  • Error handling: Comprehensive FetchError and AbortError handling

Lightweight Design

Common Use Cases

DeFi Protocol Research

Market Analysis Follow-up

Error Handling

Both implementations include comprehensive error handling:

Common Error Types

  • Connection failures: Network issues or invalid endpoints
  • Authentication errors: Invalid API keys (401 status codes)
  • Timeout errors: Long-running requests that exceed the 5-minute limit
  • JSON parsing errors: Malformed responses from the server
  • Stream interruptions: Graceful handling of interrupted connections

TypeScript-Specific Error Handling

The TypeScript client provides enhanced error detection:

Signal Handling

The TypeScript implementation includes graceful shutdown on process signals:

Next Steps

Remember to keep your API key secure and never commit it to version control. Always use environment variables for sensitive configuration.