From 768e4a7fd4dc71d2732a326f8b0946afdcffd0da Mon Sep 17 00:00:00 2001 From: Rock2z <44712825+Rock2z@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:22:18 +0800 Subject: [PATCH] [community][fix] Compatibility support to bump up wikibase-rest-api-client version (#27316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description:** This PR addresses the `TypeError: sequence item 0: expected str instance, FluentValue found` error when invoking `WikidataQueryRun`. The root cause was an incompatible version of the `wikibase-rest-api-client`, which caused the tool to fail when handling `FluentValue` objects instead of strings. The current implementation only supports `wikibase-rest-api-client<0.2`, but the latest version is `0.2.1`, where the current implementation breaks. Additionally, the error message advises users to install the latest version: [code reference](https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/utilities/wikidata.py#L125C25-L125C32). Therefore, this PR updates the tool to support the latest version of `wikibase-rest-api-client`. Key changes: - Updated the handling of `FluentValue` objects to ensure compatibility with the latest `wikibase-rest-api-client`. - Removed the restriction to `wikibase-rest-api-client<0.2` and updated to support the latest version (`0.2.1`). **Issue:** Fixes [#24093](https://github.com/langchain-ai/langchain/issues/24093) – `TypeError: sequence item 0: expected str instance, FluentValue found`. **Dependencies:** - Upgraded `wikibase-rest-api-client` to the latest version to resolve the issue. --------- Co-authored-by: peiwen_zhang Co-authored-by: Erick Friis Co-authored-by: Chester Curme --- docs/docs/integrations/tools/wikidata.ipynb | 6 +++--- libs/community/langchain_community/utilities/wikidata.py | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/docs/integrations/tools/wikidata.ipynb b/docs/docs/integrations/tools/wikidata.ipynb index f316108b80a..8ec1d88e3ac 100644 --- a/docs/docs/integrations/tools/wikidata.ipynb +++ b/docs/docs/integrations/tools/wikidata.ipynb @@ -15,7 +15,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3d9195d4", + "id": "d622c581", "metadata": { "vscode": { "languageId": "shellscript" @@ -23,7 +23,7 @@ }, "outputs": [], "source": [ - "%pip install --upgrade --quiet \"wikibase-rest-api-client<0.2\" mediawikiapi" + "%pip install --upgrade --quiet wikibase-rest-api-client mediawikiapi" ] }, { @@ -110,7 +110,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.12.5" } }, "nbformat": 4, diff --git a/libs/community/langchain_community/utilities/wikidata.py b/libs/community/langchain_community/utilities/wikidata.py index 3b2d8777662..4e2e3c06977 100644 --- a/libs/community/langchain_community/utilities/wikidata.py +++ b/libs/community/langchain_community/utilities/wikidata.py @@ -147,7 +147,9 @@ class WikidataAPIWrapper(BaseModel): doc_lines.append(f"Aliases: {', '.join(resp.aliases)}") for prop, values in resp.statements.items(): if values: - doc_lines.append(f"{prop.label}: {', '.join(values)}") + doc_lines.append( + f"{prop.label}: {', '.join([v.value or 'unknown' for v in values])}" + ) return Document( page_content=("\n".join(doc_lines))[: self.doc_content_chars_max],