添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account TypeError: sequence item 0: expected str instance, FluentValue found - Error while invoking WikidataQueryRun #24093 TypeError: sequence item 0: expected str instance, FluentValue found - Error while invoking WikidataQueryRun #24093 venkee70 opened this issue Jul 10, 2024 · 4 comments · May be fixed by #27316
  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • Example Code

    from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun
    wikidata_tool = WikidataQueryRun(api_wrapper=WikidataAPIWrapper())
    wikidata_tool.name, wikidata_tool.description

    print(wikidata_tool.run("Alan Turing"))

    Error Message and Stack Trace (if applicable)

    TypeError Traceback (most recent call last)
    Cell In[16], line 1
    ----> 1 print(wikidata_tool.run("Alan Touring"))

    File ~.virtualenvs\udemy-llm-agents-juqR4cf2\lib\site-packages\langchain_core\tools.py:452, in BaseTool.run(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, config, **kwargs)
    450 except (Exception, KeyboardInterrupt) as e:
    451 run_manager.on_tool_error(e)
    --> 452 raise e
    453 else:
    454 run_manager.on_tool_end(observation, color=color, name=self.name, **kwargs)

    File ~.virtualenvs\udemy-llm-agents-juqR4cf2\lib\site-packages\langchain_core\tools.py:409, in BaseTool.run(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, config, **kwargs)
    406 parsed_input = self._parse_input(tool_input)
    407 tool_args, tool_kwargs = self._to_args_and_kwargs(parsed_input)
    408 observation = (
    --> 409 context.run(
    410 self._run, *tool_args, run_manager=run_manager, **tool_kwargs
    411 )
    412 if new_arg_supported
    413 else context.run(self._run, *tool_args, **tool_kwargs)
    414 )
    415 except ValidationError as e:
    416 if not self.handle_validation_error:

    File ~.virtualenvs\udemy-llm-agents-juqR4cf2\lib\site-packages\langchain_community\tools\wikidata\tool.py:30, in WikidataQueryRun._run(self, query, run_manager)
    24 def _run(
    25 self,
    26 query: str,
    27 run_manager: Optional[CallbackManagerForToolRun] = None,
    28 ) -> str:
    29 """Use the Wikidata tool."""
    ---> 30 return self.api_wrapper.run(query)

    File ~.virtualenvs\udemy-llm-agents-juqR4cf2\lib\site-packages\langchain_community\utilities\wikidata.py:177, in WikidataAPIWrapper.run(self, query)
    175 docs = []
    176 for item in items[: self.top_k_results]:
    --> 177 if doc := self._item_to_document(item):
    178 docs.append(f"Result {item}:\n{doc.page_content}")
    179 if not docs:

    File ~.virtualenvs\udemy-llm-agents-juqR4cf2\lib\site-packages\langchain_community\utilities\wikidata.py:149, in WikidataAPIWrapper._item_to_document(self, qid)
    147 for prop, values in resp.statements.items():
    148 if values:
    --> 149 doc_lines.append(f"{prop.label}: {', '.join(values)}")
    151 return Document(
    152 page_content=("\n".join(doc_lines))[: self.doc_content_chars_max],
    153 meta={"title": qid, "source": f" https://www.wikidata.org/wiki/{qid}"} ,
    154 )

    TypeError: sequence item 0: expected str instance, FluentValue found

    Description

    I'm trying to use various tools from the 'langchain' library.
    Call to wikidata_tool.run("string") resulted in the above error.

    Identified a bug in the below code ( using latest version of the build)
    https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/utilities/wikidata.py

    Line 149: doc_lines.append(f"{prop.label}: {', '.join(values)}")

    /*** source of error, here 'values' is a list not a string ***/

    So I commented above code and made the below changes. the code worked fine after that.
    doc_lines.append(f"{prop.label}: {', '.join(map(str,values))}")

    System Info

    PIP freeze | grep langchain output

    langchain==0.2.7
    langchain-anthropic==0.1.19
    langchain-cohere==0.1.9
    langchain-community==0.2.7
    langchain-core==0.2.12
    langchain-experimental==0.0.62
    langchain-google-genai==1.0.4
    langchain-openai==0.1.14
    langchain-text-splitters==0.2.2
    langchainhub==0.1.20

    python -m langchain_core.sys_info

    System Information

    > OS: Windows
    > OS Version: 10.0.19045
    **> Python Version: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]

    Package Information

    langchain_core: 0.2.12
    langchain: 0.2.7
    langchain_community: 0.2.7
    langsmith: 0.1.84
    langchain_anthropic: 0.1.19
    langchain_cohere: 0.1.9
    langchain_experimental: 0.0.62
    langchain_google_genai: 1.0.4
    langchain_openai: 0.1.14
    langchain_text_splitters: 0.2.2
    langchainhub: 0.1.20
    langgraph: 0.0.51

    Packages not installed (Not Necessarily a Problem)

    The following packages were not found:

    langserve

    dependency graph.txt

    Hi @venkee70
    I encountered the same issue, and I discovered the root cause is an incompatible version of wikibase-rest-api-client .
    As outlined in the documentation here , the required version should be below 0.2. To resolve this, you can use the following:

    pip install --upgrade --quiet "wikibase-rest-api-client<0.2" mediawikiapi
    

    Alternatively, you can simply try this in notebook:

    %pip install --upgrade --quiet "wikibase-rest-api-client<0.2" mediawikiapi
    from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun
    wikidata = WikidataQueryRun(api_wrapper=WikidataAPIWrapper())
    print(wikidata.run("Alan Turing"))
    

    cc @derenrich May I ask for your advice on whether we should update the Wikidata tool to support the latest version of wikibase-rest-api-client? If so, I'd be happy to submit a PR to address this.

    Hey. Yeah sorry for the delay. To bring this up to the most recent version we just need to change

    doc_lines.append(f"{prop.label}: {', '.join(values)}")
    doc_lines.append(f"{prop.label}: {', '.join([v.value or 'unknown' for v in values])}")

    basically values turned from a list of strings to a list of FluentValue objects which is defined as

    class FluentValue:
        value: Optional[str]
        qid: Optional[str] = None
        pid: Optional[str] = None