Step 4 – Organize and summarize web results
After retrieving information from the web, organize it into a clean, readable format. This step takes the search results and compiles the key excerpts into a simple summary for evaluation.
python
import textwrap
from typing import List, Dict, Any
def build_evidence_context(results: List[Dict[str, Any]], max_chars: int = 8000) -> str:
blocks = []
for idx, r in enumerate(results):
excerpts_text = "\n\n".join(r["excerpts"][:2])
block = textwrap.dedent(f"""
[Source {idx+1}]
Title: {r['title'] or r['url']}
URL: {r['url']}
Publish date: {r['publish_date']}
Excerpts:
{excerpts_text}
""").strip()
blocks.append(block)
context = "\n\n".join(blocks)
if len(context) > max_chars:
context = context[:max_chars] + "\n\n[Context truncated for length]"
return context