A comprehensive research system implementing two complementary OpenAI approaches for automated research: custom OpenAI Agents orchestration and native Deep Research API integration.
This project demonstrates how to build production-ready research systems using OpenAI's latest capabilities. We've implemented and compared two distinct approaches:
π€ OpenAI Agents System: Build your own research workflow using OpenAI's official agents SDK with custom orchestration, agent handoffs, and real-time streaming. Perfect for scenarios requiring fine-grained control over the research process.
π¬ Native Deep Research API: Leverage OpenAI's specialized research models (o3-deep-research, o4-mini-deep-research) that provide professional-grade research reports with comprehensive citations and web search integration.
π― Unified OpenAI Intelligence: Both OpenAI approaches work together through an intelligent interface that automatically selects the best method based on query complexity, or allows manual override for specific use cases.
- Flexibility: Choose between speed (OpenAI Agents) and comprehensiveness (Deep Research API)
- Production Ready: Includes error handling, fallbacks, and real-world deployment considerations
- Extensible: Clean architecture allows easy addition of new research methods
- Educational: Complete implementation with lessons learned and best practices
β
Dual OpenAI Implementation: Both OpenAI Agents and native Deep Research API working seamlessly
β
Intelligent Routing: Auto-selects optimal OpenAI method based on query characteristics
β
Professional Citations: Rich metadata with exact text positions and excerpts
β
Error Resilience: Graceful handling of API limitations and organization requirements
β
Real-time Streaming: Progress indicators and transparent research process
β
Production Considerations: Security, scalability, cost optimization, and monitoring
- Introduction
- How to Use
- System Architecture
- Functional Specifications
- Technical Specifications
- Installation and Setup
- Usage Examples
- Non-Functional Specifications
- Future Enhancements
- Support and Troubleshooting
-
Install and Configure
pip install openai-agents>=0.0.19 openai>=1.88 pydantic export OPENAI_API_KEY="your-api-key-here"
-
Run Validation Test
python validation_test.py
-
Start Researching
python openai_research_interface.py
Option 1: Let the system choose the best OpenAI method (Recommended)
from openai_research_interface import ResearchInterface
# Initialize with auto-selection
interface = ResearchInterface()
# Ask any research question
result = await interface.research(
"What are the latest developments in LLM orchestration frameworks?"
)
print(f"Method used: {result.method_used}")
print(f"Citations: {result.metadata.get('citations_count', 0)}")
print(result.result)Option 2: Use specific OpenAI research method
from openai_research_interface import ResearchInterface, ResearchMethod
interface = ResearchInterface()
# Force OpenAI Agents for quick technical questions
quick_result = await interface.research(
"How to handle errors in LangChain?",
method=ResearchMethod.OPENAI_AGENTS
)
# Force Deep Research API for comprehensive analysis
deep_result = await interface.research(
"Analyze the competitive landscape of AI agent frameworks",
method=ResearchMethod.DEEP_RESEARCH_API
)Custom Configuration
# Configure specific models and parameters
interface = ResearchInterface(
method=ResearchMethod.DEEP_RESEARCH_API,
deep_research_model="o4-mini-deep-research-2025-06-26" # Faster model
)
# Add custom system instructions
result = await interface.research(
"Research question here",
system_message="You are a technical analyst. Focus on implementation details and code examples.",
summary="detailed" # For comprehensive reasoning steps
)Access Individual OpenAI Systems
# Use OpenAI Agents system directly
from openai_agents_research import DeepResearchSystem
openai_agents_system = DeepResearchSystem()
result = await openai_agents_system.basic_research("Your query")
# Use Deep Research API directly
from openai_deep_research_api import OpenAIDeepResearchAPI
api = OpenAIDeepResearchAPI(model="o3-deep-research-2025-06-26")
result = await api.research("Your query")When to Use Each Method
| Use Case | Recommended OpenAI Method | Why |
|---|---|---|
| Quick technical questions | OpenAI Agents System | Faster response (30-60s), good for specific answers |
| Comprehensive research | Deep Research API | Professional reports with rich citations (2-5 min) |
| Exploratory research | Auto-selection | Let the system choose based on query complexity |
| Production applications | Unified Interface | Consistent API with intelligent fallbacks |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OpenAI Research Interface β
β (openai_research_interface.py) β
β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββ β
β β Auto-Selection β β Method Override β β
β β Logic β β Capability β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
βββββββββββββ΄βββββββββ ββ΄βββββββββββββββββββ
β β β β
βββββββββΌβββββββββββ ββββββββΌβββββββββββββββ
β OpenAI Agents β β Deep Research API β
β System β β (Native OpenAI) β
β β β β
β Triage Agent β β o3-deep-research β
β Clarify Agent β β o4-mini-deep-researchβ
β Instruction Agentβ β β
β Research Agent β β Professional Reportsβ
β β β Rich Citations β
β Real-time Stream β β Web Search β
β OpenAI SDK β β Optimized Service β
ββββββββββββββββββββ βββββββββββββββββββββββ
Purpose: Custom orchestration using OpenAI's official agents SDK
Architecture:
- Triage Agent (
gpt-4o-mini): Routes queries based on complexity - Clarifying Agent (
gpt-4o-mini): Asks follow-up questions with structured output - Instruction Agent (
gpt-4o-mini): Converts queries to detailed research briefs - Research Agent (
gpt-4o): Performs comprehensive web-based research
Features:
- Real-time event streaming with progress indicators
- Custom agent handoff logic with conditional routing
- WebSearchTool integration for current information
- Structured output using Pydantic models
- Citation extraction and formatting
Input/Output:
# Input: Natural language query
query = "How do LLM orchestration frameworks handle production deployment?"
# Output: Structured research result
result = {
"text": "Comprehensive research findings...",
"agent_flow": ["Triage", "Instruction", "Research"],
"sources": ["url1", "url2", ...],
"streaming_events": [...]
}Purpose: Native OpenAI Deep Research service integration
Models:
o3-deep-research-2025-06-26: Most capable, comprehensive analysiso4-mini-deep-research-2025-06-26: Faster, efficient for simpler queries
Features:
- Professional-grade research reports
- Rich citation metadata with text excerpts and positions
- Web search integration with query tracking
- Reasoning step extraction (requires verified organization)
- Automatic fallback handling for organization verification
API Structure:
response = client.responses.create(
model="o3-deep-research-2025-06-26",
input=[
{"role": "developer", "content": [{"type": "input_text", "text": system_message}]},
{"role": "user", "content": [{"type": "input_text", "text": query}]}
],
reasoning={"summary": "auto"}, # Optional for verified orgs
tools=[
{"type": "web_search_preview"},
{"type": "code_interpreter", "container": {"type": "auto", "file_ids": []}}
]
)Purpose: Intelligent routing and consistent API across both OpenAI methods
Auto-Selection Logic:
def _auto_select_method(self, query: str) -> ResearchMethod:
# Complex, comprehensive queries β Deep Research API
complex_keywords = ["landscape", "comprehensive", "analysis", "trends"]
# Specific technical queries β OpenAI Agents System
technical_keywords = ["how to", "implement", "specific", "technical"]
# Fallback to available methodUsage Patterns:
# Auto-selection
interface = ResearchInterface()
result = await interface.research("Your query")
# Method override
result = await interface.research("Query", method=ResearchMethod.DEEP_RESEARCH_API)
# Configuration
interface = ResearchInterface(
method=ResearchMethod.AUTO,
deep_research_model="o4-mini-deep-research-2025-06-26"
)class Citation(BaseModel):
title: str # Source title
url: str # Source URL
start_index: int # Character position in text
end_index: int # End character position
excerpt: str # Extracted text snippetclass UnifiedResearchResult(BaseModel):
query: str # Original query
method_used: str # "multi_agent" | "deep_research_api"
result: str # Research findings
metadata: Dict[str, Any] # Method-specific metadatapip install openai-agents>=0.0.19 openai>=1.88 pydantic python-dotenv# Required
export OPENAI_API_KEY="your-api-key-here"
# Optional
export OPENAI_AGENTS_DISABLE_TRACING="1" # For zero data retentionβββ test_deep_research.py # Original mock test suite
βββ openai_agents_research.py # OpenAI Agents SDK implementation
βββ openai_deep_research_api.py # Deep Research API wrapper
βββ openai_research_interface.py # Unified OpenAI interface with auto-selection
βββ validation_test.py # Component validation tests
βββ .env.example # Environment template
βββ IMPLEMENTATION_SUMMARY.md # Technical achievements summary
βββ README.md # This documentation
| Metric | OpenAI Agents System | Deep Research API |
|---|---|---|
| Response Time | 30-60 seconds | 2-5 minutes |
| API Calls | 4-6 (agent pipeline) | 1 (optimized service) |
| Citation Quality | Basic extraction | Professional with excerpts |
| Customization | High (full agent control) | Medium (API parameters) |
| Cost | Multiple model calls | Single service call |
| Reliability | Depends on agent handoffs | Native service stability |
git clone <repository>
cd OpenAI_DeepResearch
pip install -r requirements.txtcp .env.example .env
# Edit .env with your OpenAI API key
export OPENAI_API_KEY="your-key-here"python validation_test.py# OpenAI unified interface (recommended)
python openai_research_interface.py
# OpenAI Agents system only
python openai_agents_research.py
# Deep Research API only
python openai_deep_research_api.pyfrom openai_research_interface import ResearchInterface
interface = ResearchInterface()
result = await interface.research(
"What are the best practices for deploying LLM applications in production?"
)
print(f"Method used: {result.method_used}")
print(f"Result: {result.result}")
print(f"Citations: {result.metadata.get('citations_count', 0)}")from openai_research_interface import ResearchInterface, ResearchMethod
# Use OpenAI Agents for technical questions
result = await interface.research(
"How to implement error handling in LangChain?",
method=ResearchMethod.OPENAI_AGENTS
)
# Use Deep Research API for comprehensive analysis
result = await interface.research(
"Analyze the competitive landscape of LLM orchestration tools",
method=ResearchMethod.DEEP_RESEARCH_API,
summary="detailed"
)# Configure with specific OpenAI model
interface = ResearchInterface(
method=ResearchMethod.DEEP_RESEARCH_API,
deep_research_model="o4-mini-deep-research-2025-06-26"
)
# Add custom system message
result = await interface.research(
query="Your research question",
system_message="You are a technical analyst focusing on implementation details..."
)Challenge: OpenAI agents SDK has different patterns than expected
- Method signatures differ from documentation (
starting_agentvsagent) - Event streaming requires defensive programming with
hasattr()checks - Async patterns need careful handling (some methods sync, others async)
Solution: Implement robust error handling and API exploration
# Defensive event handling
if hasattr(event, 'type') and hasattr(event, 'new_agent'):
print(f"Agent: {event.new_agent.name}")Challenge: Reasoning summaries require verified organizations Impact: 400 errors for unverified accounts
Solution: Graceful fallback mechanism
try:
response = client.responses.create(reasoning={"summary": "auto"}, ...)
except Exception as e:
if "must be verified" in str(e):
response = client.responses.create(...) # Without reasoningFinding: Deep Research API provides superior quality but takes 2-5 minutes OpenAI Agents: Faster (30-60s) but requires more orchestration complexity
Best Practice: Use auto-selection based on query complexity
# Route complex queries to Deep Research API
if any(word in query.lower() for word in ["comprehensive", "landscape", "analysis"]):
return ResearchMethod.DEEP_RESEARCH_APIUnified Interface Pattern:
- Single entry point for multiple implementations
- Consistent result format regardless of underlying method
- Intelligent routing based on query characteristics
- Method override capability for specific use cases
Benefits:
- Future-proof (easy to add new research methods)
- User-friendly (simple API regardless of complexity)
- Optimized (automatic best-method selection)
Layered Approach:
# Level 1: API-specific errors
try:
response = client.responses.create(...)
except OrganizationVerificationError:
# Fallback without reasoning
# Level 2: Method-level errors
except DeepResearchAPIError:
# Fall back to multi-agent system
# Level 3: System-level errors
except Exception as e:
# Return error response with contextProfessional Standards:
- Extract exact text positions (
start_index,end_index) - Include source metadata (title, URL, excerpt)
- Preserve citation context for verification
- Format for both human reading and programmatic use
Environment-First Approach:
# Prefer environment variables
api_key = os.getenv("OPENAI_API_KEY")
# Support parameter override
def __init__(self, api_key: Optional[str] = None):
self.api_key = api_key or os.getenv("OPENAI_API_KEY")Component Validation:
- Test each system independently
- Validate integration points
- Test error conditions and fallbacks
- Performance benchmarking for method selection
OpenAI Agents System: Scales with OpenAI API limits, manage concurrent agent calls Deep Research API: Natural rate limiting due to research complexity
Strategy: Use cheaper models for routing decisions
# Use gpt-4o-mini for triage, gpt-4o for research
triage_agent = Agent(model="gpt-4o-mini", ...)
research_agent = Agent(model="gpt-4o", ...)Key Metrics:
- Response times by method
- Citation quality scores
- Error rates and fallback usage
- Cost per research query
Data Handling:
- Zero data retention mode:
OPENAI_AGENTS_DISABLE_TRACING=1 - API key management through environment variables
- Input sanitization for research queries
# Cache research results for repeated queries
@lru_cache(maxsize=100)
def research_cached(query_hash: str) -> UnifiedResearchResult:
return self.research(query)OpenAI Agents: Real-time progress updates Deep Research: Background processing with status checks
Timeout Configuration:
client = OpenAI(timeout=600.0) # 10 minutes for deep researchAdd Model Context Protocol support for internal document search:
tools=[
{"type": "web_search_preview"},
{"type": "mcp", "server_url": "https://your-mcp-server/sse/"}
]Implement clarification and rewriting workflow:
# Clarification β Rewriting β Research
clarified = await clarify_query(original_query)
enhanced = await rewrite_query(clarified)
result = await research(enhanced)Machine learning-based method selection:
# Train classifier on query characteristics vs optimal method
method = ml_classifier.predict(query_features)Combine results from multiple methods:
# Run both methods for critical queries
multi_result = await multi_agent_research(query)
deep_result = await deep_research_api(query)
synthesized = synthesize_results([multi_result, deep_result])- Organization not verified for reasoning: Expected for new accounts, system handles gracefully
- Timeout errors: Deep Research API can take 2-5 minutes, adjust timeouts accordingly
- Agent handoff failures: Check API key permissions and rate limits
- Import errors: Ensure
openai-agents>=0.0.19andopenai>=1.88
# Enable verbose logging
result = await interface.research(query, verbose=True)
# Test individual components
python validation_test.py
# Check API connectivity
python -c "from openai import OpenAI; print(OpenAI().models.list())"System Status: β Production Ready - OpenAI-Focused Implementation Last Updated: 2025-06-30 Implementation: Complete with dual OpenAI research capability