Skip to main content

Overview

This guide walks through building an interactive research application using interactions. By chaining interaction_id values across requests, each follow-up question automatically has the full context of prior turns — so you can drill deeper into a topic without restating what was already researched.

Example: Multi-Turn Research Session

Consider a research session where each question builds on the last:
  1. “Which country won the most Winter Olympics gold medals in 2026?” — initial broad question
  2. “How many medals did they win?” — a follow-up that only makes sense with context from step 1
  3. “How does that compare to the second place country?” — drills deeper into the same thread
Without interactions, each request would be independent and the API wouldn’t know what “they” or “second place” refers to. With interactions, context carries forward automatically.
import os
from parallel import Parallel
from parallel.types import TaskSpecParam, TextSchemaParam

client = Parallel(api_key=os.environ["PARALLEL_API_KEY"])

# Turn 1: Initial question
run1 = client.task_run.create(
    input="Which country won the most Winter Olympics gold medals in 2026?",
    processor="lite",
    task_spec=TaskSpecParam(output_schema=TextSchemaParam()),
)
result1 = client.task_run.result(run1.run_id, api_timeout=3600)
print(f"Turn 1: {result1.output.content}")

# Turn 2: Follow-up — "they" refers to the country from Turn 1
run2 = client.task_run.create(
    input="How many medals did they win?",
    processor="lite",
    previous_interaction_id=run1.interaction_id,
    task_spec=TaskSpecParam(output_schema=TextSchemaParam()),
)
result2 = client.task_run.result(run2.run_id, api_timeout=3600)
print(f"Turn 2: {result2.output.content}")

# Turn 3: Drill deeper — context from both prior turns is available
run3 = client.task_run.create(
    input="How does that compare to the second place country?",
    processor="lite",
    previous_interaction_id=run2.interaction_id,
    task_spec=TaskSpecParam(output_schema=TextSchemaParam()),
)
result3 = client.task_run.result(run3.run_id, api_timeout=3600)
print(f"Turn 3: {result3.output.content}")

Building a Research Chat Loop

Wrap the interaction pattern in a loop to create a fully interactive research agent. Each user question chains off the previous answer’s context.
import os
from parallel import Parallel
from parallel.types import TaskSpecParam, TextSchemaParam

client = Parallel(api_key=os.environ["PARALLEL_API_KEY"])

interaction_id = None

while True:
    user_input = input("You: ")
    if user_input.lower() in ("exit", "quit"):
        break

    task_run = client.task_run.create(
        input=user_input,
        processor="lite",
        task_spec=TaskSpecParam(output_schema=TextSchemaParam()),
        **({"previous_interaction_id": interaction_id} if interaction_id else {}),
    )

    result = client.task_run.result(task_run.run_id, api_timeout=3600)
    interaction_id = task_run.interaction_id

    print(f"\nAgent: {result.output.content}\n")

Next Steps

  • Deep Research: Comprehensive single-turn research — includes an example of chatting with deep research results
  • Interactions: Full reference for the interaction_id and previous_interaction_id fields
  • Enrichment: Structured data enrichment
  • Streaming Events: Monitor long-running research tasks in real time