Overview
The Grok Search service (despite its name, it uses OpenAI’s web search tool) finds local service providers based on job requirements. It searches the web for businesses near the user’s location and returns structured provider data with names and phone numbers. Source:services/grok_search.py
This service is named “grok_search” but actually uses OpenAI’s Responses API with the
web_search_preview tool, not xAI’s Grok.API Functions
search_providers()
Searches for service providers using OpenAI’s web search tool.Complete Job object containing all job details. Required fields:
id: Job ID for associating providerstask: Service type (e.g., “plumber”, “electrician”)zip_code: Location to search near- Other job metadata (problem, context_answers, etc.)
List of provider objects, each containing:
job_id: The job ID this provider is associated withname: Business namephone: Phone number in various formats
MAX_PROVIDERS (default: 5) providers.- Constructs search prompt using
build_search_prompt() - Calls OpenAI Responses API with
web_search_previewtool - Receives web search results as text
- Parses provider data using
parse_provider_response() - Returns structured provider objects
- Falls back to mock data if API unavailable
build_search_prompt()
Constructs the search query string from job details.Job object with task and zip_code fields
Formatted search prompt string optimized for web search
- Service type and location
- Business name and phone number only
NAME | PHONEformatting- Maximum number of results (controlled by
MAX_PROVIDERSconfig)
parse_provider_response()
Parses web search response text into structured provider objects.Raw response content from OpenAI web search
Job ID to associate with parsed providers
Parsed provider objects with name and phone
-
Phone Number Detection: Uses regex to find phone numbers in multiple formats
(xxx) xxx-xxxxxxx-xxx-xxxxxxx.xxx.xxxxxxxxxxxxxx
- Name Extraction: Extracts business name before the phone number
-
Cleanup Operations:
- Removes numbered prefixes (“1.”, “1)”, “1:”)
- Removes markdown formatting (asterisks)
- Removes leading/trailing separators (dash, pipe, colon)
- Skips header lines and too-short names
-
Validation:
- Name must be at least 3 characters
- Line must contain a valid phone number
- Skips generic header words (“name”, “business”, “provider”)
Configuration
Environment Variables
Your OpenAI API key for accessing web search
OpenAI organization key (if applicable)
Maximum number of providers to return (default: 5)
Thread Pool Configuration
The service uses a thread pool to avoid blocking the async event loop:Implementation Details
OpenAI Web Search Call
gpt-4oTool:
web_search_preview (searches the web and returns formatted results)Output: Text response with provider names and phone numbers
Fallback Providers
When the API is unavailable or returns no results, the service provides realistic mock data:- Plumber: Reliable Plumbing Services, Quick Drain Solutions, Bay Area Master Plumbers, etc.
- Electrician: Bright Spark Electric, Safe Home Electrical, PowerUp Electricians, etc.
- House Cleaner: Sparkle Clean Services, Maid Perfect, Home Fresh Cleaning, etc.
- Painter: Pro Coat Painters, Fresh Paint Co., Color Masters Painting, etc.
- Default: Generic handyman/service providers for unlisted tasks
- Realistic business name
- Phone number in
(408) 555-XXXXformat - Mock estimated price (not used in current implementation)
Error Handling
The service gracefully handles all API errors:- Missing API key → Returns fallback providers
- Network error → Returns fallback providers
- Rate limiting → Returns fallback providers
- Parse failure → Returns fallback providers
- Empty results → Returns fallback providers
Phone Number Regex
The service uses a comprehensive phone number pattern:(408) 555-0101408-555-0101408.555.0101408 555 01014085550101
- International formats
- Extensions
- Incomplete numbers
Integration Example
Complete workflow from job creation to provider search:Performance Considerations
Async Handling:- Limited to 2 concurrent searches (thread pool size)
- No built-in retry logic (fails to fallback)
- Consider implementing rate limiting if calling frequently
- Web search: 3-8 seconds typical
- Fallback: < 100ms
- Parsing: < 10ms
See Also
- Grok LLM Service - Task inference and question generation
- Voice Agent - Automated calling to providers
- OpenAI Web Search Docs - Official API documentation