Agent Foundry
All Problems

#92. Parallel Branch and Merge

MediumOrchestration

The Problem

Your text-analysis pipeline runs sentiment analysis and entity extraction sequentially—one after the other. These two tasks are completely independent, so the pipeline takes roughly twice as long as it needs to. Your job is to refactor the pipeline into a parallel fan-out/fan-in pattern: both analyses run at the same time, and a merge step combines their results once both are done.

Examples

Example 1

User input: Apple CEO Tim Cook announced the new iPhone at their Cupertino headquarters. Customers loved it.

Current (bad) output: Results are correct, but processing takes ~2s because sentiment runs first, then entities run after.

Expected (good) output: Both analyses run in parallel (~1s total). The merged output:

{
  "sentiment": "positive — customers loved the announcement",
  "entities": ["Apple", "Tim Cook", "iPhone", "Cupertino"],
  "processing_time": "1.05s"
}

Example 2

User input: The project in Berlin failed miserably. Google and Microsoft both pulled out of the partnership.

Current (bad) output: Sequential processing returns correct results but with unnecessary latency.

Expected (good) output: Parallel processing completes faster. The merged output contains both the sentiment (negative) and entities (Berlin, Google, Microsoft) in a single structured response.

Your Task

Refactor the starter code so that:

  • Sentiment analysis and entity extraction run in parallel as separate branches.
  • A merge step waits for both branches to complete before combining results.
  • The final output is a single structured object containing both sentiment and entities.
  • The parallel execution provides a measurable speedup over the sequential baseline.

Evaluation

Submissions are checked for the following:

  • Parallel execution: Sentiment analysis and entity extraction run concurrently, not one after the other.
  • Both complete before merge: The merge step waits for both branches to finish before combining results.
  • Structured merged output: The final output contains both sentiment and entities in a single structured result.

Constraints

  • Sentiment analysis and entity extraction must run in parallel, not sequentially
  • Both branches must complete before the merge step begins
  • The merge step must combine results from both branches into a single structured output
  • The parallel execution must produce a measurable speedup over sequential processing
Starter Code
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
import time

llm = ChatOpenAI(model="gpt-4o-mini")

# BUG: Sentiment and entity extraction run sequentially, doubling latency
# TODO: Run both analyses in parallel and merge results
def analyze_text(text: str) -> dict:
    start = time.time()

    sentiment = llm.invoke([
        SystemMessage(content="Analyze the sentiment of this text. Reply with: positive, negative, or neutral, followed by a brief explanation."),
        HumanMessage(content=text),
    ])

    entities = llm.invoke([
        SystemMessage(content="Extract all named entities (people, organizations, locations, products) from this text. Return as a JSON list."),
        HumanMessage(content=text),
    ])

    elapsed = time.time() - start
    return {
        "sentiment": sentiment.content,
        "entities": entities.content,
        "processing_time": f"{elapsed:.2f}s",
    }

texts = [
    "Apple CEO Tim Cook announced the new iPhone at their Cupertino headquarters. Customers loved it.",
    "The project in Berlin failed miserably. Google and Microsoft both pulled out of the partnership.",
]
for text in texts:
    result = analyze_text(text)
    print(f"Text: {text[:50]}...\nResult: {result}\n")
Open in Google Colab
Evaluation Criteria0/3