Skip to content

Commit 3306f22

Browse files
committed
Wolfram|Alpha lib assertion error -> Add manual API call
1 parent a8d2bd4 commit 3306f22

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

src/api/wolfram_client.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def process_query(self, query: str) -> list[ResultType]:
3636
"""Main query execution method"""
3737
try:
3838
res = await self.client.aquery(str(query))
39-
39+
return await self.process_results(res)
4040
except Exception:
4141
logging.warning("Wolfram|Alpha lib assertion error -> Using manual API call")
4242
timeout = httpx.Timeout(30.0, read=30.0)
@@ -45,25 +45,18 @@ async def process_query(self, query: str) -> list[ResultType]:
4545
"input": str(query)
4646
}
4747
res = None
48-
max_retries = 4
49-
attempt = 0
50-
# fix: correct retry logic and result assignment for WolframAlpha API requests
51-
while res is None and attempt < max_retries:
48+
max_retries = 4
49+
for attempt in range(1, max_retries + 1):
5250
try:
5351
async with httpx.AsyncClient(timeout=timeout) as client:
5452
resp = await client.get(self.client.url, params=params)
55-
if resp.status_code == 200 and resp.content:
56-
res = xmltodict.parse(resp.content, postprocessor=Document.make)['queryresult']
57-
break
53+
res = xmltodict.parse(resp.content, postprocessor=Document.make)['queryresult']
54+
if res:
55+
return await self.process_results(res)
5856
except Exception as e:
59-
logging.warning(f"error: {e} Timeout, retrying...")
60-
attempt += 1
57+
logging.warning(f"Attempt {attempt} failed: {e}. Retrying in 2s...")
6158
await asyncio.sleep(2)
62-
63-
if res is None:
64-
raise RuntimeError("Failed to get a valid response from WolframAlpha API after several retries.")
65-
else:
66-
return await self.process_results(res)
59+
raise RuntimeError("Failed to get a valid response from WolframAlpha API after several retries.")
6760

6861
async def process_results(self, res) -> list[ResultType]:
6962
"""Process results into text/image formats"""

0 commit comments

Comments
 (0)