Skip to content

Commit 66e94ad

Browse files
author
stlc-bot
committed
feat(news): add natural-language search to POST /news/search (#1975)
Stainless-Generated-From: eba10bdeeea904662e36a89fc6ac8b0a9e6fbaac
1 parent abdca3a commit 66e94ad

3 files changed

Lines changed: 76 additions & 36 deletions

File tree

‎src/context/dev/resources/news.py‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ def search(
6161
extra_body: Body | None = None,
6262
timeout: float | httpx.Timeout | None | NotGiven = not_given,
6363
) -> NewsSearchResponse:
64-
"""Find company news by name, domain, ticker, or ISIN.
65-
66-
Filter articles and continue
67-
through results with a cursor.
64+
"""
65+
Find news about a company by name, domain, ticker, or ISIN, or search all news
66+
with a natural-language query. Filter articles and continue through results with
67+
a cursor.
6868
6969
Args:
70-
search_by: What to search for.
70+
search_by: What to search for: a specific company, or a natural-language query across all
71+
news.
7172
7273
cursor: Opaque next_cursor from the previous response, or null for the first page.
7374
@@ -77,7 +78,8 @@ def search(
7778
7879
limit: Maximum results to return. Defaults to 10.
7980
80-
sort_by: Result ordering. Defaults to newest.
81+
sort_by: Result ordering. Defaults to newest for entity searches. Natural-language
82+
searches are always ordered by relevance.
8183
8284
tags: Labels for filtering usage in the dashboard.
8385
@@ -147,13 +149,14 @@ async def search(
147149
extra_body: Body | None = None,
148150
timeout: float | httpx.Timeout | None | NotGiven = not_given,
149151
) -> NewsSearchResponse:
150-
"""Find company news by name, domain, ticker, or ISIN.
151-
152-
Filter articles and continue
153-
through results with a cursor.
152+
"""
153+
Find news about a company by name, domain, ticker, or ISIN, or search all news
154+
with a natural-language query. Filter articles and continue through results with
155+
a cursor.
154156
155157
Args:
156-
search_by: What to search for.
158+
search_by: What to search for: a specific company, or a natural-language query across all
159+
news.
157160
158161
cursor: Opaque next_cursor from the previous response, or null for the first page.
159162
@@ -163,7 +166,8 @@ async def search(
163166
164167
limit: Maximum results to return. Defaults to 10.
165168
166-
sort_by: Result ordering. Defaults to newest.
169+
sort_by: Result ordering. Defaults to newest for entity searches. Natural-language
170+
searches are always ordered by relevance.
167171
168172
tags: Labels for filtering usage in the dashboard.
169173

‎src/context/dev/types/news_search_params.py‎

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
__all__ = [
1212
"NewsSearchParams",
1313
"SearchBy",
14-
"SearchByEntity",
15-
"SearchByEntityNewsSearchEntityByName",
16-
"SearchByEntityNewsSearchEntityByDomain",
17-
"SearchByEntityNewsSearchEntityByTicker",
18-
"SearchByEntityNewsSearchEntityByIsin",
14+
"SearchByNewsSearchByNaturalLanguage",
15+
"SearchByNewsSearchByEntity",
16+
"SearchByNewsSearchByEntityEntity",
17+
"SearchByNewsSearchByEntityEntityNewsSearchEntityByName",
18+
"SearchByNewsSearchByEntityEntityNewsSearchEntityByDomain",
19+
"SearchByNewsSearchByEntityEntityNewsSearchEntityByTicker",
20+
"SearchByNewsSearchByEntityEntityNewsSearchEntityByIsin",
1921
"FilterBy",
2022
"FilterByDate",
2123
"SortBy",
@@ -24,7 +26,10 @@
2426

2527
class NewsSearchParams(TypedDict, total=False):
2628
search_by: Required[Annotated[SearchBy, PropertyInfo(alias="searchBy")]]
27-
"""What to search for."""
29+
"""
30+
What to search for: a specific company, or a natural-language query across all
31+
news.
32+
"""
2833

2934
cursor: Optional[str]
3035
"""Opaque next_cursor from the previous response, or null for the first page."""
@@ -40,13 +45,33 @@ class NewsSearchParams(TypedDict, total=False):
4045
"""Maximum results to return. Defaults to 10."""
4146

4247
sort_by: Annotated[SortBy, PropertyInfo(alias="sortBy")]
43-
"""Result ordering. Defaults to newest."""
48+
"""Result ordering.
49+
50+
Defaults to newest for entity searches. Natural-language searches are always
51+
ordered by relevance.
52+
"""
4453

4554
tags: SequenceNotStr[str]
4655
"""Labels for filtering usage in the dashboard."""
4756

4857

49-
class SearchByEntityNewsSearchEntityByName(TypedDict, total=False):
58+
class SearchByNewsSearchByNaturalLanguage(TypedDict, total=False):
59+
"""Search all news with a plain-language query.
60+
61+
Returns up to 100 matches, ordered by relevance.
62+
"""
63+
64+
query: Required[str]
65+
"""
66+
What to search for, in plain language, such as "chip export restrictions to
67+
China".
68+
"""
69+
70+
type: Required[Literal["natural_language"]]
71+
"""Use `natural_language` to search all news with a plain-language query."""
72+
73+
74+
class SearchByNewsSearchByEntityEntityNewsSearchEntityByName(TypedDict, total=False):
5075
"""Identify the company by name."""
5176

5277
name: Required[str]
@@ -56,7 +81,7 @@ class SearchByEntityNewsSearchEntityByName(TypedDict, total=False):
5681
"""Use `name` to identify the company by name."""
5782

5883

59-
class SearchByEntityNewsSearchEntityByDomain(TypedDict, total=False):
84+
class SearchByNewsSearchByEntityEntityNewsSearchEntityByDomain(TypedDict, total=False):
6085
"""Identify the company by website domain."""
6186

6287
domain: Required[str]
@@ -66,7 +91,7 @@ class SearchByEntityNewsSearchEntityByDomain(TypedDict, total=False):
6691
"""Use `domain` to identify the company by website domain."""
6792

6893

69-
class SearchByEntityNewsSearchEntityByTicker(TypedDict, total=False):
94+
class SearchByNewsSearchByEntityEntityNewsSearchEntityByTicker(TypedDict, total=False):
7095
"""Identify the company by stock ticker, optionally scoped to an exchange."""
7196

7297
ticker: Required[str]
@@ -155,7 +180,7 @@ class SearchByEntityNewsSearchEntityByTicker(TypedDict, total=False):
155180
"""
156181

157182

158-
class SearchByEntityNewsSearchEntityByIsin(TypedDict, total=False):
183+
class SearchByNewsSearchByEntityEntityNewsSearchEntityByIsin(TypedDict, total=False):
159184
"""Identify the company by International Securities Identification Number."""
160185

161186
isin: Required[str]
@@ -165,24 +190,26 @@ class SearchByEntityNewsSearchEntityByIsin(TypedDict, total=False):
165190
"""Use `isin` to identify the company by its securities identifier."""
166191

167192

168-
SearchByEntity: TypeAlias = Union[
169-
SearchByEntityNewsSearchEntityByName,
170-
SearchByEntityNewsSearchEntityByDomain,
171-
SearchByEntityNewsSearchEntityByTicker,
172-
SearchByEntityNewsSearchEntityByIsin,
193+
SearchByNewsSearchByEntityEntity: TypeAlias = Union[
194+
SearchByNewsSearchByEntityEntityNewsSearchEntityByName,
195+
SearchByNewsSearchByEntityEntityNewsSearchEntityByDomain,
196+
SearchByNewsSearchByEntityEntityNewsSearchEntityByTicker,
197+
SearchByNewsSearchByEntityEntityNewsSearchEntityByIsin,
173198
]
174199

175200

176-
class SearchBy(TypedDict, total=False):
177-
"""What to search for."""
201+
class SearchByNewsSearchByEntity(TypedDict, total=False):
202+
"""Find news about a specific company."""
178203

179-
entity: Required[SearchByEntity]
204+
entity: Required[SearchByNewsSearchByEntityEntity]
180205
"""The company to search news for, identified by name, domain, ticker, or ISIN."""
181206

182207
type: Required[Literal["entity"]]
183-
"""How to search. Only entity search is supported."""
208+
"""Use `entity` to find news about a specific company."""
184209

185210

211+
SearchBy: TypeAlias = Union[SearchByNewsSearchByNaturalLanguage, SearchByNewsSearchByEntity]
212+
186213
_FilterByDateReservedKeywords = TypedDict(
187214
"_FilterByDateReservedKeywords",
188215
{
@@ -337,7 +364,10 @@ class FilterBy(TypedDict, total=False):
337364

338365

339366
class SortBy(TypedDict, total=False):
340-
"""Result ordering. Defaults to newest."""
367+
"""Result ordering.
368+
369+
Defaults to newest for entity searches. Natural-language searches are always ordered by relevance.
370+
"""
341371

342372
type: Required[Literal["relevance", "newest"]]
343373
"""Result ordering."""

‎src/context/dev/types/news_search_response.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111

1212
class DataMatch(BaseModel):
13-
"""How the article relates to the company you searched for."""
13+
"""How the article relates to the company you searched for.
14+
15+
Null for natural-language searches.
16+
"""
1417

1518
confidence: Optional[float] = None
1619
"""How confident the match is, from 0 to 1. Null when a score is unavailable."""
@@ -57,8 +60,11 @@ class Data(BaseModel):
5760
Null when unknown.
5861
"""
5962

60-
match: DataMatch
61-
"""How the article relates to the company you searched for."""
63+
match: Optional[DataMatch] = None
64+
"""How the article relates to the company you searched for.
65+
66+
Null for natural-language searches.
67+
"""
6268

6369
published_at: Optional[datetime] = None
6470
"""When the article was published, as an ISO 8601 timestamp.

0 commit comments

Comments
 (0)