From 663a4147943824649c485a4287e13bafbba1e323 Mon Sep 17 00:00:00 2001 From: shortcuts Date: Fri, 29 Aug 2025 15:53:33 +0200 Subject: [PATCH 1/2] fix(python): remove warnings for HighlightResult and SnippetResult --- playground/python/app/search.py | 67 ++++++++++++------------- playground/python/poetry.lock | 2 +- specs/search/common/schemas/Hit.yml | 1 + templates/python/imports.mustache | 1 + templates/python/model_generic.mustache | 30 +++++++++++ 5 files changed, 66 insertions(+), 35 deletions(-) diff --git a/playground/python/app/search.py b/playground/python/app/search.py index 4945d1dec40..816e945228b 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -11,48 +11,47 @@ def main(): print("SearchClient version", __version__) - # client = SearchClientSync( + client = SearchClientSync( + environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") + ) + client.add_user_agent("playground") + client.add_user_agent("bar", "baz") + + print("user_agent", client._config._user_agent.get()) + print("client initialized", client) + + try: + resp = client.search_single_index("poussing-RECORDS") + print(resp.to_dict()) + finally: + client.close() + + print("client closed") + + # print("with transformations") + # + # config = SearchConfig( # environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") # ) - # client.add_user_agent("playground") - # client.add_user_agent("bar", "baz") + # + # config.set_transformation_region("eu") + # + # print("region in playground") + # print(config.region) + # + # client = SearchClientSync.create_with_config(config) + # client.add_user_agent("playground search with ingestion") # # print("user_agent", client._config._user_agent.get()) - # print("client initialized", client) # # try: - # resp = client.search_synonyms("foo") + # resp = client.replace_all_objects_with_transformation( + # "boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2 + # ) # print(resp) - # client.browse_synonyms("foo", lambda _resp: print(_resp)) + # except Exception as e: + # print(e) # finally: # client.close() # # print("client closed") - # - # print("with transformations") - # - config = SearchConfig( - environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") - ) - - config.set_transformation_region("eu") - - print("region in playground") - print(config.region) - - client = SearchClientSync.create_with_config(config) - client.add_user_agent("playground search with ingestion") - - print("user_agent", client._config._user_agent.get()) - - try: - resp = client.replace_all_objects_with_transformation( - "boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2 - ) - print(resp) - except Exception as e: - print(e) - finally: - client.close() - - print("client closed") diff --git a/playground/python/poetry.lock b/playground/python/poetry.lock index 144c811ab7a..23fd4d14feb 100644 --- a/playground/python/poetry.lock +++ b/playground/python/poetry.lock @@ -142,7 +142,7 @@ frozenlist = ">=1.1.0" [[package]] name = "algoliasearch" -version = "4.20.0" +version = "4.25.0" description = "A fully-featured and blazing-fast Python API client to interact with Algolia." optional = false python-versions = ">= 3.8.1" diff --git a/specs/search/common/schemas/Hit.yml b/specs/search/common/schemas/Hit.yml index e9eef29a2d6..efb5f5b2e83 100644 --- a/specs/search/common/schemas/Hit.yml +++ b/specs/search/common/schemas/Hit.yml @@ -5,6 +5,7 @@ hit: A hit is a record from your index, augmented with special attributes for highlighting, snippeting, and ranking. x-is-generic: true + x-is-hit-object: true additionalProperties: true required: - objectID diff --git a/templates/python/imports.mustache b/templates/python/imports.mustache index ee8d083ef28..29a9b531b55 100644 --- a/templates/python/imports.mustache +++ b/templates/python/imports.mustache @@ -24,6 +24,7 @@ from pydantic import ( StrictStr, ValidationError, field_validator, + field_serializer, model_serializer, ) from typing import ( diff --git a/templates/python/model_generic.mustache b/templates/python/model_generic.mustache index 25defe31eb9..e2087f32639 100644 --- a/templates/python/model_generic.mustache +++ b/templates/python/model_generic.mustache @@ -79,6 +79,36 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} extra='allow', ) + {{#vendorExtensions.x-is-hit-object}} + @staticmethod + def __dump_item(item): + return item.model_dump(warnings="none") if isinstance(item, BaseModel) else item + + @field_serializer("highlight_result") + def serialize_highlight_result(self, v: HighlightResult | None): + if v is None: + return None + + if isinstance(v, dict): + return {k: self.__dump_item(val) for k, val in v.items()} + elif isinstance(v, list): + return [self.__dump_item(val) for val in v] + else: + return self.__dump_item(v) + + @field_serializer("snippet_result") + def serialize_snippet_result(self, v: SnippetResult | None): + if v is None: + return None + + if isinstance(v, dict): + return {k: self.__dump_item(val) for k, val in v.items()} + elif isinstance(v, list): + return [self.__dump_item(val) for val in v] + else: + return self.__dump_item(v) + {{/vendorExtensions.x-is-hit-object}} + def to_json(self) -> str: return self.model_dump_json(by_alias=True, exclude_unset=True) From c685e2abf02301c9a79eb3a15a1524b8326e667a Mon Sep 17 00:00:00 2001 From: shortcuts Date: Fri, 29 Aug 2025 16:00:40 +0200 Subject: [PATCH 2/2] fix: dump config --- templates/python/model_generic.mustache | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/templates/python/model_generic.mustache b/templates/python/model_generic.mustache index e2087f32639..dcfb98df584 100644 --- a/templates/python/model_generic.mustache +++ b/templates/python/model_generic.mustache @@ -82,7 +82,12 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{#vendorExtensions.x-is-hit-object}} @staticmethod def __dump_item(item): - return item.model_dump(warnings="none") if isinstance(item, BaseModel) else item + return item.model_dump( + by_alias=True, + exclude_none=True, + exclude_unset=True, + warnings="none", + ) if isinstance(item, BaseModel) else item @field_serializer("highlight_result") def serialize_highlight_result(self, v: HighlightResult | None):