Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(egress): split firecrawl search into focused helpers (#108892)

posthog[bot]6a255a40b089
diff --git a/posthog/egress/firecrawl/client.py b/posthog/egress/firecrawl/client.py
index 9f851c5fe84e..894b67a6cd00 100644
--- a/posthog/egress/firecrawl/client.py
+++ b/posthog/egress/firecrawl/client.py
@@ -10,6 +10,8 @@ from typing import Literal, cast
from django.conf import settings
+import requests
+
from posthog.dataclasses import frozen
from posthog.egress.firecrawl.limiter import consume_firecrawl_sync
from posthog.egress.firecrawl.transport import FirecrawlEgressBudgetExhausted, firecrawl_request
@@ -112,6 +114,67 @@ def _as_sequence(value: object) -> Sequence[object]:
return value
+def _require_api_key() -> str:
+ api_key = settings.FIRECRAWL_API_KEY
+ if not api_key:
+ raise FirecrawlNotConfigured("No FIRECRAWL_API_KEY configured")
+ return api_key
+
+
+@frozen
+class _SuccessfulBody:
+ payload: Mapping[str, object]
+ data: Mapping[str, object]
+
+
+def _successful_body(response: requests.Response, subject: str, error: type[Exception]) -> _SuccessfulBody:
+ """Return the top-level payload and its ``data`` object, or raise ``error`` when Firecrawl did
+ not answer with a successful body."""
+ if not response.ok:
+ # The body can carry the scraped page, so keep it out of the exception that gets logged.
+ raise error(f"{subject} returned HTTP {response.status_code}")
+
+ try:
+ body: object = response.json()
+ except ValueError as exc:
+ raise error(f"{subject} returned a non-JSON body") from exc
+
+ payload = _as_mapping(body)
+ if payload is None or payload.get("success") is not True:
+ raise error(f"{subject} was unsuccessful")
+
+ data = _as_mapping(payload.get("data"))
+ if data is None:
+ raise error(f"{subject} returned no data")
+
+ return _SuccessfulBody(payload=payload, data=data)
+
+
+def _check_search_input(query: str, limit: int) -> None:
+ if limit > MAX_SEARCH_LIMIT:
+ raise ValueError(f"limit must be at most {MAX_SEARCH_LIMIT}")
+ if len(query) > MAX_SEARCH_QUERY_CHARS:
+ raise ValueError(f"query must be at most {MAX_SEARCH_QUERY_CHARS} characters")
+
+
+def _search_result(entry: object) -> FirecrawlSearchResult | None:
+ entry_mapping = _as_mapping(entry)
+ if entry_mapping is None:
+ return None
+ url = _as_str(entry_mapping.get("url"))
+ if url is None:
+ return None
+ return FirecrawlSearchResult(
+ url=url,
+ title=_as_str(entry_mapping.get("title")),
+ description=_as_str(entry_mapping.get("description")),
+ )
+
+
+def _search_results(web: object) -> tuple[FirecrawlSearchResult, ...]:
+ return tuple(result for entry in _as_sequence(web) if (result := _search_result(entry)) is not None)
+
+
def scrape(
url: str,
*,
@@ -129,37 +192,17 @@ def scrape(
:class:`~posthog.egress.firecrawl.transport.FirecrawlEgressBudgetExhausted` when our own egress
budget sheds the call.
"""
- api_key = settings.FIRECRAWL_API_KEY
- if not api_key:
- raise FirecrawlNotConfigured("No FIRECRAWL_API_KEY configured")
-
response = firecrawl_request(
"POST",
f"{FIRECRAWL_API_BASE}{SCRAPE_ENDPOINT}",
- api_key=api_key,
+ api_key=_require_api_key(),
source=source,
endpoint=SCRAPE_ENDPOINT,
priority=priority,
timeout=timeout,
json={"url": url, "formats": list(formats), "onlyMainContent": only_main_content},
)
-
- if not response.ok:
- # The body can carry the scraped page, so keep it out of the exception that gets logged.
- raise FirecrawlScrapeFailed(f"Firecrawl scrape of {url} returned HTTP {response.status_code}")
-
- try:
- payload: object = response.json()
- except ValueError as exc:
- raise FirecrawlScrapeFailed(f"Firecrawl scrape of {url} returned a non-JSON body") from exc
-
- payload_mapping = _as_mapping(payload)
- if payload_mapping is None or payload_mapping.get("success") is not True:
- raise FirecrawlScrapeFailed(f"Firecrawl scrape of {url} was unsuccessful")
-
- data = _as_mapping(payload_mapping.get("data"))
- if data is None:
- raise FirecrawlScrapeFailed(f"Firecrawl scrape of {url} returned no data")
+ data = _successful_body(response, f"Firecrawl scrape of {url}", FirecrawlScrapeFailed).data
metadata = _as_mapping(data.get("metadata")) or {}
return FirecrawlScrape(
@@ -189,14 +232,8 @@ def search(
:class:`~posthog.egress.firecrawl.transport.FirecrawlEgressBudgetExhausted` when our own egress
budget sheds the call.
"""
- if limit > MAX_SEARCH_LIMIT:
- raise ValueError(f"limit must be at most {MAX_SEARCH_LIMIT}")
- if len(query) > MAX_SEARCH_QUERY_CHARS:
- raise ValueError(f"query must be at most {MAX_SEARCH_QUERY_CHARS} characters")
-
- api_key = settings.FIRECRAWL_API_KEY
- if not api_key:
- raise FirecrawlNotConfigured("No FIRECRAWL_API_KEY configured")
+ _check_search_input(query, limit)
+ api_key = _require_api_key()
if not consume_firecrawl_sync(1, priority=priority, source=source):
raise FirecrawlEgressBudgetExhausted("Firecrawl search egress budget exhausted; degrading")
@@ -211,41 +248,10 @@ def search(
timeout=timeout,
json={"query": query, "limit": limit, "sources": [{"type": "web"}]},
)
-
- if not response.ok:
- raise FirecrawlSearchFailed(f"Firecrawl search for {query!r} returned HTTP {response.status_code}")
-
- try:
- payload: object = response.json()
- except ValueError as exc:
- raise FirecrawlSearchFailed(f"Firecrawl search for {query!r} returned a non-JSON body") from exc
-
- payload_mapping = _as_mapping(payload)
- if payload_mapping is None or payload_mapping.get("success") is not True:
- raise FirecrawlSearchFailed(f"Firecrawl search for {query!r} was unsuccessful")
-
- data = _as_mapping(payload_mapping.get("data"))
- if data is None:
- raise FirecrawlSearchFailed(f"Firecrawl search for {query!r} returned no data")
-
- results = []
- for entry in _as_sequence(data.get("web")):
- entry_mapping = _as_mapping(entry)
- if entry_mapping is None:
- continue
- url = _as_str(entry_mapping.get("url"))
- if url is None:
- continue
- results.append(
- FirecrawlSearchResult(
- url=url,
- title=_as_str(entry_mapping.get("title")),
- description=_as_str(entry_mapping.get("description")),
- )
- )
+ body = _successful_body(response, f"Firecrawl search for {query!r}", FirecrawlSearchFailed)
return FirecrawlSearch(
query=query,
- results=tuple(results),
- credits_used=_as_int(payload_mapping.get("creditsUsed")),
+ results=_search_results(body.data.get("web")),
+ credits_used=_as_int(body.payload.get("creditsUsed")),
)
diff --git a/posthog/egress/test/test_firecrawl.py b/posthog/egress/test/test_firecrawl.py
index eb95d9f09126..ab7d802800d9 100644
--- a/posthog/egress/test/test_firecrawl.py
+++ b/posthog/egress/test/test_firecrawl.py
@@ -157,6 +157,7 @@ class TestFirecrawlSearchEgress(SimpleTestCase):
"web": [
{"url": "https://example.com/widgets", "title": "Widgets", "description": "All the widgets."},
{"title": "No url here"},
+ "not an object",
],
},
"creditsUsed": 2,