Skip to content

Commit 4c7d899

Browse files
shixishclaude
andcommitted
feat(dql): named params type, col, filter_exclude, exportseparator, exportfile, jsonmode, non_canonical_facts, no_dedup_articles, cluster
Keyword-only options on dql()/dql_async() (and so dql_parallel), sent in the POST body. Explicit option plus the same wire key in extra raises ValidationError. report/view stay reachable via extra only. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 45e7cdc commit 4c7d899

6 files changed

Lines changed: 256 additions & 18 deletions

File tree

‎src/diffbot/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def _installed_version(dist: str):
5151
from .types import (
5252
ChatMessage,
5353
CrawlJobStatusCode,
54+
DqlCluster,
55+
DqlJsonMode,
56+
DqlOptions,
57+
DqlType,
5458
ExportFormat,
5559
ResponseFormat,
5660
SnippetLength,
@@ -111,6 +115,10 @@ def _installed_version(dist: str):
111115
"RetryPolicy",
112116
"ChatMessage",
113117
"ResponseFormat",
118+
"DqlCluster",
119+
"DqlJsonMode",
120+
"DqlOptions",
121+
"DqlType",
114122
"ExportFormat",
115123
"SnippetLength",
116124
"CrawlJobStatusCode",

‎src/diffbot/client.py‎

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
CrawlJob,
2828
CustomExtractOptions,
2929
DiscussionExtractOptions,
30+
DqlCluster,
31+
DqlJsonMode,
32+
DqlOptions,
33+
DqlType,
3034
DiscussionExtractResponse,
3135
DqlEntity,
3236
DqlResponse,
@@ -339,27 +343,27 @@ def crawl_delete_job(self, job_name: str) -> None:
339343

340344
@overload
341345
def dql(
342-
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[True],
346+
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[True], **options: "Unpack[DqlOptions]"
343347
) -> bytes: ...
344348
@overload
345349
def dql(
346-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["csv"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
350+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["csv"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
347351
) -> str: ...
348352
@overload
349353
def dql(
350-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["xls", "xlsx"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
354+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["xls", "xlsx"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
351355
) -> bytes: ...
352356
@overload
353357
def dql(
354-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["jsonl"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
358+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["jsonl"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
355359
) -> Iterator[DqlEntity]: ...
356360
@overload
357361
def dql(
358-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["json"] = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
362+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["json"] = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
359363
) -> DqlResponse: ...
360364
@overload
361365
def dql(
362-
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: bool = ...
366+
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: bool = ..., **options: "Unpack[DqlOptions]"
363367
) -> Union[DqlResponse, str, bytes, Iterator[DqlEntity]]: ...
364368
def dql(
365369
self,
@@ -372,15 +376,35 @@ def dql(
372376
exportspec: Optional[str] = None,
373377
extra: Optional[Mapping[str, Any]] = None,
374378
raw: bool = False,
379+
type: Optional[DqlType] = None,
380+
col: Optional[str] = None,
381+
filter_exclude: Optional[str] = None,
382+
exportseparator: Optional[str] = None,
383+
exportfile: Optional[str] = None,
384+
jsonmode: Optional[DqlJsonMode] = None,
385+
non_canonical_facts: Optional[bool] = None,
386+
no_dedup_articles: Optional[bool] = None,
387+
cluster: Optional[DqlCluster] = None,
375388
) -> Any:
376389
"""Run a DQL query against the Diffbot Knowledge Graph (sent as ``POST`` with a JSON body).
377390
378391
Return type follows the arguments: ``DqlResponse`` for ``format="json"``
379392
(default); ``str`` for ``"csv"``; ``bytes`` for ``"xls"``/``"xlsx"`` or any
380393
format with ``raw=True`` (the undecoded response); an iterator of
381394
``DqlEntity`` for ``"jsonl"``.
395+
396+
Named options (all optional, sent in the POST body): ``type``
397+
(``"query"``/``"text"``/``"queryTextFallback"``/``"crawl"``; ``"crawl"`` searches
398+
crawl collections, selected with ``col``), ``filter_exclude``, ``exportseparator``,
399+
``exportfile``, ``jsonmode``, ``non_canonical_facts``, ``no_dedup_articles`` and
400+
``cluster``. A key given both as a named option and in ``extra`` raises
401+
``ValidationError``. ``report`` and ``view`` are not named; use ``extra``.
382402
"""
383-
return _dql(self, query, size=size, from_=from_, format=format, filter=filter, exportspec=exportspec, extra=extra, raw=raw)
403+
return _dql(self, query, size=size, from_=from_, format=format, filter=filter, exportspec=exportspec, extra=extra, raw=raw,
404+
type=type, col=col, filter_exclude=filter_exclude, exportseparator=exportseparator,
405+
exportfile=exportfile, jsonmode=jsonmode, non_canonical_facts=non_canonical_facts,
406+
no_dedup_articles=no_dedup_articles, cluster=cluster,
407+
)
384408

385409
def dql_parallel(self, queries: Sequence[Dict[str, Any]], *, workers: int = 8) -> List[Any]:
386410
"""Run multiple DQL queries concurrently. Each item is a dict of dql() keyword args;
@@ -662,27 +686,27 @@ async def crawl_delete_job(self, job_name: str) -> None:
662686

663687
@overload
664688
async def dql(
665-
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[True],
689+
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[True], **options: "Unpack[DqlOptions]"
666690
) -> bytes: ...
667691
@overload
668692
async def dql(
669-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["csv"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
693+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["csv"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
670694
) -> str: ...
671695
@overload
672696
async def dql(
673-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["xls", "xlsx"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
697+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["xls", "xlsx"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
674698
) -> bytes: ...
675699
@overload
676700
async def dql(
677-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["jsonl"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
701+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["jsonl"], filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
678702
) -> AsyncIterator[DqlEntity]: ...
679703
@overload
680704
async def dql(
681-
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["json"] = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ...
705+
self, query: str, *, size: int = ..., from_: int = ..., format: Literal["json"] = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: Literal[False] = ..., **options: "Unpack[DqlOptions]"
682706
) -> DqlResponse: ...
683707
@overload
684708
async def dql(
685-
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: bool = ...
709+
self, query: str, *, size: int = ..., from_: int = ..., format: ExportFormat = ..., filter: Optional[str] = ..., exportspec: Optional[str] = ..., extra: Optional[Mapping[str, Any]] = ..., raw: bool = ..., **options: "Unpack[DqlOptions]"
686710
) -> Union[DqlResponse, str, bytes, AsyncIterator[DqlEntity]]: ...
687711
async def dql(
688712
self,
@@ -695,15 +719,35 @@ async def dql(
695719
exportspec: Optional[str] = None,
696720
extra: Optional[Mapping[str, Any]] = None,
697721
raw: bool = False,
722+
type: Optional[DqlType] = None,
723+
col: Optional[str] = None,
724+
filter_exclude: Optional[str] = None,
725+
exportseparator: Optional[str] = None,
726+
exportfile: Optional[str] = None,
727+
jsonmode: Optional[DqlJsonMode] = None,
728+
non_canonical_facts: Optional[bool] = None,
729+
no_dedup_articles: Optional[bool] = None,
730+
cluster: Optional[DqlCluster] = None,
698731
) -> Any:
699732
"""Run a DQL query against the Diffbot Knowledge Graph (sent as ``POST`` with a JSON body).
700733
701734
Return type follows the arguments: ``DqlResponse`` for ``format="json"``
702735
(default); ``str`` for ``"csv"``; ``bytes`` for ``"xls"``/``"xlsx"`` or any
703736
format with ``raw=True`` (the undecoded response); an iterator of
704737
``DqlEntity`` for ``"jsonl"``.
738+
739+
Named options (all optional, sent in the POST body): ``type``
740+
(``"query"``/``"text"``/``"queryTextFallback"``/``"crawl"``; ``"crawl"`` searches
741+
crawl collections, selected with ``col``), ``filter_exclude``, ``exportseparator``,
742+
``exportfile``, ``jsonmode``, ``non_canonical_facts``, ``no_dedup_articles`` and
743+
``cluster``. A key given both as a named option and in ``extra`` raises
744+
``ValidationError``. ``report`` and ``view`` are not named; use ``extra``.
705745
"""
706-
return await _dql_async(self, query, size=size, from_=from_, format=format, filter=filter, exportspec=exportspec, extra=extra, raw=raw)
746+
return await _dql_async(self, query, size=size, from_=from_, format=format, filter=filter, exportspec=exportspec, extra=extra, raw=raw,
747+
type=type, col=col, filter_exclude=filter_exclude, exportseparator=exportseparator,
748+
exportfile=exportfile, jsonmode=jsonmode, non_canonical_facts=non_canonical_facts,
749+
no_dedup_articles=no_dedup_articles, cluster=cluster,
750+
)
707751

708752
async def dql_parallel(self, queries: Sequence[Dict[str, Any]], *, workers: int = 8) -> List[Any]:
709753
"""Run multiple DQL queries concurrently. Each item is a dict of dql() keyword args;

‎src/diffbot/kg.py‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, Iterator, List, Mapping, Optional, Sequence
88

99
from ._constants import KG_BASE
10+
from .errors import ValidationError
1011
from .ontology import Ontology
11-
from .types import DqlEntity, ExportFormat
12+
from .types import DqlCluster, DqlEntity, DqlJsonMode, DqlType, ExportFormat
1213

1314
if TYPE_CHECKING:
1415
from .client import Diffbot, DiffbotAsync
@@ -25,6 +26,7 @@ def _build_dql_body(
2526
filter: Optional[str],
2627
exportspec: Optional[str],
2728
extra: Optional[Mapping[str, Any]],
29+
named: Optional[Mapping[str, Any]] = None,
2830
) -> Dict[str, Any]:
2931
"""JSON body for ``POST /kg/v3/dql`` (``DqlPostRequest``). The token is *not* part
3032
of the body; it travels in the query string, as the spec's security scheme says."""
@@ -37,11 +39,46 @@ def _build_dql_body(
3739
body["filter"] = filter
3840
if exportspec is not None:
3941
body["exportspec"] = exportspec
42+
if named:
43+
# ``named`` maps wire names to values; ``None`` means "not set".
44+
set_named = {k: v for k, v in named.items() if v is not None}
45+
if extra:
46+
dupes = sorted(set(set_named) & set(extra))
47+
if dupes:
48+
raise ValidationError(
49+
f"{', '.join(dupes)} given both as a named option and in extra; pass it once"
50+
)
51+
body.update(set_named)
4052
if extra:
4153
body.update(extra)
4254
return body
4355

4456

57+
def _named_dql_options(
58+
type: Optional[DqlType],
59+
col: Optional[str],
60+
filter_exclude: Optional[str],
61+
exportseparator: Optional[str],
62+
exportfile: Optional[str],
63+
jsonmode: Optional[DqlJsonMode],
64+
non_canonical_facts: Optional[bool],
65+
no_dedup_articles: Optional[bool],
66+
cluster: Optional[DqlCluster],
67+
) -> Dict[str, Any]:
68+
"""Python names -> wire names for the named DQL options."""
69+
return {
70+
"type": type,
71+
"col": col,
72+
"filterExclude": filter_exclude,
73+
"exportseparator": exportseparator,
74+
"exportfile": exportfile,
75+
"jsonmode": jsonmode,
76+
"nonCanonicalFacts": non_canonical_facts,
77+
"noDedupArticles": no_dedup_articles,
78+
"cluster": cluster,
79+
}
80+
81+
4582
def _decode_dql(response: Any, format: ExportFormat, raw: bool) -> Any:
4683
"""Decode a DQL response by ``format``: json -> dict, csv -> str, xls/xlsx -> bytes,
4784
jsonl -> iterator of entities. ``raw=True`` always returns the undecoded bytes."""
@@ -79,8 +116,21 @@ def dql(
79116
exportspec: Optional[str] = None,
80117
extra: Optional[Mapping[str, Any]] = None,
81118
raw: bool = False,
119+
type: Optional[DqlType] = None,
120+
col: Optional[str] = None,
121+
filter_exclude: Optional[str] = None,
122+
exportseparator: Optional[str] = None,
123+
exportfile: Optional[str] = None,
124+
jsonmode: Optional[DqlJsonMode] = None,
125+
non_canonical_facts: Optional[bool] = None,
126+
no_dedup_articles: Optional[bool] = None,
127+
cluster: Optional[DqlCluster] = None,
82128
) -> Any:
83-
body = _build_dql_body(query, size, from_, format, filter, exportspec, extra)
129+
named = _named_dql_options(
130+
type, col, filter_exclude, exportseparator, exportfile, jsonmode,
131+
non_canonical_facts, no_dedup_articles, cluster,
132+
)
133+
body = _build_dql_body(query, size, from_, format, filter, exportspec, extra, named)
84134
response = client.request("POST", client.dql_url, params={"token": client.token}, json=body)
85135
return _decode_dql(response, format, raw)
86136

@@ -96,8 +146,21 @@ async def dql_async(
96146
exportspec: Optional[str] = None,
97147
extra: Optional[Mapping[str, Any]] = None,
98148
raw: bool = False,
149+
type: Optional[DqlType] = None,
150+
col: Optional[str] = None,
151+
filter_exclude: Optional[str] = None,
152+
exportseparator: Optional[str] = None,
153+
exportfile: Optional[str] = None,
154+
jsonmode: Optional[DqlJsonMode] = None,
155+
non_canonical_facts: Optional[bool] = None,
156+
no_dedup_articles: Optional[bool] = None,
157+
cluster: Optional[DqlCluster] = None,
99158
) -> Any:
100-
body = _build_dql_body(query, size, from_, format, filter, exportspec, extra)
159+
named = _named_dql_options(
160+
type, col, filter_exclude, exportseparator, exportfile, jsonmode,
161+
non_canonical_facts, no_dedup_articles, cluster,
162+
)
163+
body = _build_dql_body(query, size, from_, format, filter, exportspec, extra, named)
101164
response = await client.request("POST", client.dql_url, params={"token": client.token}, json=body)
102165
if not raw and format == "jsonl":
103166
return _aiter_jsonl(response.text)

‎src/diffbot/types.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ class ResponseFormat(TypedDict, total=False):
3535
#: DQL / Enhance-bulk export formats.
3636
ExportFormat = Literal["json", "jsonl", "csv", "xls", "xlsx"]
3737

38+
#: DQL ``type``: ``crawl`` searches your crawl collections (see ``col``).
39+
DqlType = Literal["query", "text", "queryTextFallback", "crawl"]
40+
41+
#: DQL ``jsonmode``: ``extended`` adds origin info for facts, ``id`` returns only ids and origins.
42+
DqlJsonMode = Literal["extended", "id"]
43+
44+
#: DQL ``cluster`` (Article queries only).
45+
DqlCluster = Literal["all", "best", "dedupe"]
46+
47+
48+
class DqlOptions(TypedDict, total=False):
49+
"""Named DQL options sent in the POST JSON body (keyword-only on ``dql()``).
50+
51+
``report`` and ``view`` are deliberately not named; pass them through ``extra``.
52+
"""
53+
54+
type: DqlType
55+
col: str
56+
filter_exclude: str
57+
exportseparator: str
58+
exportfile: str
59+
jsonmode: DqlJsonMode
60+
non_canonical_facts: bool
61+
no_dedup_articles: bool
62+
cluster: DqlCluster
63+
64+
3865
#: How much of each page's matching content `web_search` returns.
3966
SnippetLength = Literal["short", "medium", "long", "full"]
4067

0 commit comments

Comments
 (0)