Skip to content

Commit

Permalink
PowerBI: catch outdated token (langchain-ai#6634)
Browse files Browse the repository at this point in the history
This adds just a small tweak to catch the error that says the token is
expired rather then retrying.
  • Loading branch information
eavanvalkenburg authored Jun 23, 2023
1 parent b1de927 commit 48381f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions langchain/tools/powerbi/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def _run(
logger.info("Query: %s", query)
pbi_result = self.powerbi.run(command=query)
result, error = self._parse_output(pbi_result)
if error is not None and "TokenExpired" in error:
self.session_cache[
tool_input
] = "Authentication token expired or invalid, please try reauthenticate."
return self.session_cache[tool_input]

iterations = kwargs.get("iterations", 0)
if error and iterations < self.max_iterations:
Expand Down Expand Up @@ -140,6 +145,11 @@ async def _arun(
logger.info("Query: %s", query)
pbi_result = await self.powerbi.arun(command=query)
result, error = self._parse_output(pbi_result)
if error is not None and "TokenExpired" in error:
self.session_cache[
tool_input
] = "Authentication token expired or invalid, please try reauthenticate."
return self.session_cache[tool_input]

iterations = kwargs.get("iterations", 0)
if error and iterations < self.max_iterations:
Expand Down
4 changes: 2 additions & 2 deletions langchain/utilities/powerbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async def arun(self, command: str) -> Any:
json=self._create_json_content(command),
timeout=10,
) as response:
response_json = await response.json()
response_json = await response.json(content_type=response.content_type)
return response_json
async with aiohttp.ClientSession() as session:
async with session.post(
Expand All @@ -235,7 +235,7 @@ async def arun(self, command: str) -> Any:
json=self._create_json_content(command),
timeout=10,
) as response:
response_json = await response.json()
response_json = await response.json(content_type=response.content_type)
return response_json


Expand Down

0 comments on commit 48381f1

Please sign in to comment.