Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions litellm/llms/fastcrw/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
fastCRW API integration module.
"""

from litellm.llms.fastcrw.search.transformation import FastCRWSearchConfig

__all__ = ["FastCRWSearchConfig"]
7 changes: 7 additions & 0 deletions litellm/llms/fastcrw/search/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
fastCRW Search API module.
"""

from litellm.llms.fastcrw.search.transformation import FastCRWSearchConfig

__all__ = ["FastCRWSearchConfig"]
182 changes: 182 additions & 0 deletions litellm/llms/fastcrw/search/transformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
"""
Calls fastCRW's /v1/search endpoint to search the web.

fastCRW is a Firecrawl-compatible web data engine (single Rust binary; self-host
or cloud). The search response uses the Firecrawl-compatible envelope
{ "success": true, "data": [ { "title", "url", "description", "markdown"? } ] }.

fastCRW API Reference: https://fastcrw.com/docs/rest-api
"""

from typing import Dict, List, Optional, TypedDict, Union

import httpx

from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms.base_llm.search.transformation import (
BaseSearchConfig,
SearchResponse,
SearchResult,
)
from litellm.secret_managers.main import get_secret_str


class _FastCRWSearchRequestRequired(TypedDict):
"""Required fields for fastCRW Search API request."""

query: str # Required - search query


class FastCRWSearchRequest(_FastCRWSearchRequestRequired, total=False):
"""
fastCRW Search API request format.
Based on: https://fastcrw.com/docs/rest-api
"""

limit: int # Optional - maximum number of results to return
sources: List[
str
] # Optional - sources to search ('web', 'images'), default ['web']
scrapeOptions: Dict # Optional - options for scraping search results


class FastCRWSearchConfig(BaseSearchConfig):
FASTCRW_API_BASE = "https://fastcrw.com/api/v1"

@staticmethod
def ui_friendly_name() -> str:
return "fastCRW"

def validate_environment(
self,
headers: Dict,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
**kwargs,
) -> Dict:
"""
Validate environment and return headers.
"""
api_key = api_key or get_secret_str("CRW_API_KEY")
if not api_key:
raise ValueError(
"CRW_API_KEY is not set. Set `CRW_API_KEY` environment variable."
)
headers["Authorization"] = f"Bearer {api_key}"
headers["Content-Type"] = "application/json"
return headers

def get_complete_url(
self,
api_base: Optional[str],
optional_params: dict,
data: Optional[Union[Dict, List[Dict]]] = None,
**kwargs,
) -> str:
"""
Get complete URL for Search endpoint.
"""
api_base = api_base or get_secret_str("CRW_API_BASE") or self.FASTCRW_API_BASE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High: API key exfiltration via api_base override

validate_environment() falls back to the server's CRW_API_KEY, but this method still trusts the caller-provided api_base. A proxy caller can send search_provider=fastcrw with api_base=https://attacker.example and LiteLLM will POST to that host with Authorization: Bearer <CRW_API_KEY>; only accept api_base from trusted config/env, or only honor a caller-provided base when the caller also supplied the API key used for that request.


# Append "/search" to the api base if it's not already there
if not api_base.endswith("/search"):
api_base = f"{api_base}/search"

return api_base

def transform_search_request(
self,
query: Union[str, List[str]],
optional_params: dict,
**kwargs,
) -> Dict:
"""
Transform Search request to fastCRW API format.

Transforms Perplexity unified spec parameters:
- query -> query (same)
- max_results -> limit

All other fastCRW-specific parameters are passed through as-is.

Args:
query: Search query (string or list of strings). fastCRW only supports single string queries.
optional_params: Optional parameters for the request

Returns:
Dict with typed request data following FastCRWSearchRequest spec
"""
if isinstance(query, list):
# fastCRW only supports single string queries, join with spaces
query = " ".join(query)

request_data: FastCRWSearchRequest = {
"query": query,
}

# Transform Perplexity unified spec parameters to fastCRW format
if "max_results" in optional_params:
request_data["limit"] = optional_params["max_results"]

# Convert to dict before dynamic key assignments
result_data = dict(request_data)

# pass through all other parameters as-is
for param, value in optional_params.items():
if (
param not in self.get_supported_perplexity_optional_params()
and param not in result_data
):
result_data[param] = value

# By default, request markdown content if not explicitly specified
# fastCRW doesn't return content unless explicitly requested via scrapeOptions
if "scrapeOptions" not in result_data:
result_data["scrapeOptions"] = {
"formats": ["markdown"],
"onlyMainContent": True,
}

return result_data

def transform_search_response(
self,
raw_response: httpx.Response,
logging_obj: LiteLLMLoggingObj,
**kwargs,
) -> SearchResponse:
"""
Transform fastCRW API response to LiteLLM unified SearchResponse format.

fastCRW (Firecrawl-compatible) returns:
{"success": true, "data": [{"url": "...", "title": "...", "description": "...", "markdown"?: "..."}, ...]}

Args:
raw_response: Raw httpx response from fastCRW API
logging_obj: Logging object for tracking

Returns:
SearchResponse with standardized format
"""
response_json = raw_response.json()

results = []

data = response_json.get("data", [])

if isinstance(data, list):
for result in data:
snippet = result.get("markdown") or result.get("description", "")
search_result = SearchResult(
title=result.get("title", ""),
url=result.get("url", ""),
snippet=snippet,
date=None,
last_updated=None,
)
results.append(search_result)

return SearchResponse(
results=results,
object="search",
)
1 change: 1 addition & 0 deletions litellm/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,7 @@ class SearchProviders(str, Enum):
GOOGLE_PSE = "google_pse"
DATAFORSEO = "dataforseo"
FIRECRAWL = "firecrawl"
FASTCRW = "fastcrw"
SEARXNG = "searxng"
LINKUP = "linkup"
DUCKDUCKGO = "duckduckgo"
Expand Down
2 changes: 2 additions & 0 deletions litellm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9647,6 +9647,7 @@ def get_provider_search_config(
from litellm.llms.dataforseo.search.transformation import DataForSEOSearchConfig
from litellm.llms.duckduckgo.search.transformation import DuckDuckGoSearchConfig
from litellm.llms.exa_ai.search.transformation import ExaAISearchConfig
from litellm.llms.fastcrw.search.transformation import FastCRWSearchConfig
from litellm.llms.firecrawl.search.transformation import FirecrawlSearchConfig
from litellm.llms.google_pse.search.transformation import GooglePSESearchConfig
from litellm.llms.linkup.search.transformation import LinkupSearchConfig
Expand All @@ -9669,6 +9670,7 @@ def get_provider_search_config(
SearchProviders.GOOGLE_PSE: GooglePSESearchConfig,
SearchProviders.DATAFORSEO: DataForSEOSearchConfig,
SearchProviders.FIRECRAWL: FirecrawlSearchConfig,
SearchProviders.FASTCRW: FastCRWSearchConfig,
SearchProviders.SEARXNG: SearXNGSearchConfig,
SearchProviders.LINKUP: LinkupSearchConfig,
SearchProviders.DUCKDUCKGO: DuckDuckGoSearchConfig,
Expand Down
17 changes: 17 additions & 0 deletions provider_endpoints_support.json
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,23 @@
"search": true
}
},
"fastcrw": {
"display_name": "fastCRW (`fastcrw`)",
"url": "https://docs.litellm.ai/docs/search/fastcrw",
"endpoints": {
"chat_completions": false,
"messages": false,
"responses": false,
"embeddings": false,
"image_generations": false,
"audio_transcriptions": false,
"audio_speech": false,
"moderations": false,
"batches": false,
"rerank": false,
"search": true
}
},
"linkup": {
"display_name": "Linkup (`linkup`)",
"url": "https://docs.litellm.ai/docs/search/linkup",
Expand Down
1 change: 1 addition & 0 deletions tests/code_coverage_tests/enforce_llms_folder_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"exa_ai",
"brave",
"firecrawl",
"fastcrw",
"searxng",
"linkup",
"duckduckgo",
Expand Down
Loading
Loading