WrenAI icon indicating copy to clipboard operation
WrenAI copied to clipboard

Intermittent Syntax Error in WrenAI on Identical Input

Open UMANET123 opened this issue 7 months ago • 2 comments

Hi Team,

We have successfully implemented and configured WrenAI in our production environment, and it is working well overall. However, we are occasionally encountering a syntax error when processing a specific question. Interestingly, the same question sometimes returns the expected output without any issues.

Please find the screenshot attached for your reference.

we are using Open Al LLM API KEY.

Data connection error invalid input syntax for type bigint: "2025-03-13"

and

Required keyword: 'expression' missing for <class 'sqlglot.expressions.JSONExtract'>. Line 1, Col: 48. SELECT CONCAT(LAX_STRING(JSON_QUERY(p.firstname[4m)[0m), ' ', LAX_STRING(JSON_QUERY(p.middlename)), ' ', LAX_STRING(JSON_QUERY(p.lastname))) AS customer_n

Required keyword: 'expression' missing for <class 'sqlglot.expressions.JSONExtract'>. Line 1, Col: 91. SELECT CONCAT(LAX_STRING(JSON_QUERY(p.firstname)), ' ', LAX_STRING(JSON_QUERY(p.middlename[4m)[0m), ' ', LAX_STRING(JSON_QUERY(p.lastname))) AS customer_name, ROUND(SUM(ssh.totaldue), 2) AS total_s

Required keyword: 'expression' missing for <class 'sqlglot.expressions.JSONExtract'>. Line 1, Col: 132. UERY(p.firstname)), ' ', LAX_STRING(JSON_QUERY(p.middlename)), ' ', LAX_STRING(JSON_QUERY(p.lastname[4m)[0m)) AS customer_name, ROUND(SUM(ssh.totaldue), 2) AS total_sales_value FROM sales_salesorderheader AS

and

Exception: <class 'psycopg.errors.CannotCoerce'>, message: cannot cast type integer to timestamp with time zone LINE 1: ...ON ("so"."customerid" = "c"."customerid") WHERE ((CAST("so"....

Image

Image

Image

Image

UMANET123 avatar May 16 '25 06:05 UMANET123

@UMANET123 could you also share ibis container log? and what is your pg version? thanks

cyyeh avatar May 16 '25 06:05 cyyeh

Hi @cyyeh, Thank you for your quick response. Please find the below requested details.

We are using Postgres SQL 12. 22 psql --version psql (PostgreSQL) 12.22 (Ubuntu 12.22-2.pgdg20.04+1)

Please find the Wren-Ul error logs:

2025-05-16T06:17:24.450] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:25.451] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:26.069] [DEBUG] QueryService - Use ibis adaptor to preview [2025-05-16T06:17:26.076] [DEBUG] IbisAdaptor - Dry run sql from ibis with body: [2025-05-16T06:17:26.076] [DEBUG] IbisAdaptor - Using ibis v3 api [2025-05-16T06:17:26.222] [INFO] IbisAdaptor - Got error when dry running ibis [2025-05-16T06:17:26.452] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:26.459] [INFO] AskingTaskTracker - Updating task 83a84def-ca2a-4207-9211-447496b25e64 in database [2025-05-16T06:17:27.453] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:28.454] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:28.718] [DEBUG] QueryService - Use ibis adaptor to preview [2025-05-16T06:17:28.723] [DEBUG] IbisAdaptor - Dry run sql from ibis with body: [2025-05-16T06:17:28.723] [DEBUG] IbisAdaptor - Using ibis v3 api [2025-05-16T06:17:28.880] [INFO] IbisAdaptor - Got error when dry running ibis [2025-05-16T06:17:29.454] [INFO] AskingTaskTracker - Polling for updates for task 83a84def-ca2a-4207-9211-447496b25e64 [2025-05-16T06:17:29.459] [INFO] AskingTaskTracker - Updating task 83a84def-ca2a-4207-9211-447496b25e64 in database [2025-05-16T06:17:29.465] [INFO] AskingTaskTracker - Task 83a84def-ca2a-4207-9211-447496b25e64 is finalized with status: FAILED

As requested please find the IBIS error logs:

2025-05-16 06:59:49.061 | [b123e993-6ad5-4293-9b5d-3589d884e292] | ERROR | main.custom_http_error_handler:77 - Request failed

Traceback (most recent call last):

File "/app/mdl/rewriter.py", line 119, in rewrite

return await to_thread.run_sync(session_context.transform_sql, sql)

File "/app/.venv/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync

return await get_async_backend().run_sync_in_worker_thread(

File "/app/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2461, in run_sync_in_worker_thread

return await future

File "/app/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 962, in run

result = context.run(func, *args)

Exception: DataFusion error: Error during planning: table 'wrenai.public.person' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/routers/v3/connector.py", line 51, in query

rewritten_sql = await Rewriter(

File "/app/.venv/lib/python3.11/site-packages/opentelemetry/util/_decorator.py", line 71, in async_wrapper

return await func(*args, **kwargs)  # type: ignore

File "/app/mdl/rewriter.py", line 57, in rewrite

planned_sql = await self._rewriter.rewrite(manifest_str, sql)

File "/app/.venv/lib/python3.11/site-packages/opentelemetry/util/_decorator.py", line 71, in async_wrapper

return await func(*args, **kwargs)  # type: ignore

File "/app/mdl/rewriter.py", line 121, in rewrite

raise RewriteError(str(e))

app.mdl.rewriter.RewriteError: DataFusion error: Error during planning: table 'wrenai.public.person' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/model/connector.py", line 61, in dry_run

self._connector.dry_run(sql)

File "/usr/local/lib/python3.11/contextlib.py", line 81, in inner

return func(*args, **kwds)

File "/app/model/connector.py", line 77, in dry_run

self.connection.sql(sql)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/sql/init.py", line 179, in sql

schema = self._get_schema_using_query(query)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 547, in _get_schema_using_query

with self._safe_raw_sql(create_stmt):

File "/usr/local/lib/python3.11/contextlib.py", line 137, in enter

return next(self.gen)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 710, in _safe_raw_sql

with contextlib.closing(self.raw_sql(*args, **kwargs)) as result:

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 735, in raw_sql

cursor.execute(query, **kwargs)

File "/app/.venv/lib/python3.11/site-packages/psycopg/cursor.py", line 97, in execute

raise ex.with_traceback(None)

psycopg.errors.UndefinedTable: relation "person" does not exist

LINE 1: ...("so"."customerid" = "c"."customerid") INNER JOIN "person" A...

                                                         ^

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app

await app(scope, receive, sender)

File "/app/.venv/lib/python3.11/site-packages/starlette/routing.py", line 73, in app

response = await f(request)

File "/app/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 301, in app

raw_response = await run_endpoint_function(

File "/app/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 212, in run_endpoint_function

return await dependant.call(**values)

File "/app/routers/v3/connector.py", line 64, in query

return await v2.connector.query(

File "/app/routers/v2/connector.py", line 63, in query

connector.dry_run(rewritten_sql)

File "/app/model/connector.py", line 63, in dry_run

raise QueryDryRunError(f"Exception: {type(e)}, message: {e!s}")

app.model.connector.QueryDryRunError: Exception: <class 'psycopg.errors.UndefinedTable'>, message: relation "person" does not exist

LINE 1: ...("so"."customerid" = "c"."customerid") INNER JOIN "person" A...

                                                         ^

2025-05-16 06:59:49.069 | [b123e993-6ad5-4293-9b5d-3589d884e292] | INFO | init.dispatch:29 - Request ended

2025-05-16 06:59:48.971 | [b123e993-6ad5-4293-9b5d-3589d884e292] | WARNING | connector.query:60 - Failed to execute v3 query, fallback to v2: DataFusion error: Error during planning: table 'wrenai.public.person' not found

Wren engine is migrating to Rust version now. Wren AI team are appreciate if you can provide the error messages and related logs for us.

2025-05-16 06:59:48.979 | [b123e993-6ad5-4293-9b5d-3589d884e292] | DEBUG | rewriter.rewrite:56 - Extracted manifest: eyJjYXRhbG9nIjoid3JlbmFpIiwic2NoZW1hIjoicHVibGljIiwibW9kZWxzIjpbeyJuYW1lIjoicGVyc29uX3BlcnNvbiIsInJlZlNxbCI6bnVsbCwiYmFzZU9i

2025-05-16 06:59:40.346 | [a80951b7-ebaf-4d1f-b8e4-fe2991ca1511] | ERROR | main.custom_http_error_handler:77 - Request failed

Traceback (most recent call last):

File "/app/mdl/rewriter.py", line 119, in rewrite

return await to_thread.run_sync(session_context.transform_sql, sql)

File "/app/.venv/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync

return await get_async_backend().run_sync_in_worker_thread(

File "/app/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2461, in run_sync_in_worker_thread

return await future

File "/app/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 962, in run

result = context.run(func, *args)

Exception: DataFusion error: Error during planning: table 'wrenai.person.person' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/routers/v3/connector.py", line 51, in query

rewritten_sql = await Rewriter(

File "/app/.venv/lib/python3.11/site-packages/opentelemetry/util/_decorator.py", line 71, in async_wrapper

return await func(*args, **kwargs)  # type: ignore

File "/app/mdl/rewriter.py", line 57, in rewrite

planned_sql = await self._rewriter.rewrite(manifest_str, sql)

File "/app/.venv/lib/python3.11/site-packages/opentelemetry/util/_decorator.py", line 71, in async_wrapper

return await func(*args, **kwargs)  # type: ignore

File "/app/mdl/rewriter.py", line 121, in rewrite

raise RewriteError(str(e))

app.mdl.rewriter.RewriteError: DataFusion error: Error during planning: table 'wrenai.person.person' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/model/connector.py", line 61, in dry_run

self._connector.dry_run(sql)

File "/usr/local/lib/python3.11/contextlib.py", line 81, in inner

return func(*args, **kwds)

File "/app/model/connector.py", line 77, in dry_run

self.connection.sql(sql)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/sql/init.py", line 179, in sql

schema = self._get_schema_using_query(query)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 547, in _get_schema_using_query

with self._safe_raw_sql(create_stmt):

File "/usr/local/lib/python3.11/contextlib.py", line 137, in enter

return next(self.gen)

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 710, in _safe_raw_sql

with contextlib.closing(self.raw_sql(*args, **kwargs)) as result:

File "/app/.venv/lib/python3.11/site-packages/ibis/backends/postgres/init.py", line 735, in raw_sql

cursor.execute(query, **kwargs)

File "/app/.venv/lib/python3.11/site-packages/psycopg/cursor.py", line 97, in execute

raise ex.with_traceback(None)

psycopg.errors.UndefinedColumn: column p.customerid does not exist

LINE 1: ...N "person"."person" AS "p" ON ("c"."customerid" = "p"."custo...

                                                         ^

HINT: Perhaps you meant to reference the column "c.customerid".

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app

await app(scope, receive, sender)

File "/app/.venv/lib/python3.11/site-packages/starlette/routing.py", line 73, in app

response = await f(request)

File "/app/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 301, in app

raw_response = await run_endpoint_function(

File "/app/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 212, in run_endpoint_function

return await dependant.call(**values)

File "/app/routers/v3/connector.py", line 64, in query

return await v2.connector.query(

File "/app/routers/v2/connector.py", line 63, in query

connector.dry_run(rewritten_sql)

File "/app/model/connector.py", line 63, in dry_run

raise QueryDryRunError(f"Exception: {type(e)}, message: {e!s}")

app.model.connector.QueryDryRunError: Exception: <class 'psycopg.errors.UndefinedColumn'>, message: column p.customerid does not exist

LINE 1: ...N "person"."person" AS "p" ON ("c"."customerid" = "p"."custo...

                                                         ^

HINT: Perhaps you meant to reference the column "c.customerid".

2025-05-16 06:59:40.349 | [a80951b7-ebaf-4d1f-b8e4-fe2991ca1511] | INFO | init.dispatch:29 - Request ended

2025-05-16 06:59:48.952 | [b123e993-6ad5-4293-9b5d-3589d884e292] | INFO | init.dispatch:14 - POST /v3/connector/postgres/query

2025-05-16 06:59:48.952 | [b123e993-6ad5-4293-9b5d-3589d884e292] | INFO | init.dispatch:15 - Request params: {'dryRun': 'true'}

2025-05-16 06:59:48.953 | [b123e993-6ad5-4293-9b5d-3589d884e292] | INFO | init.dispatch:22 - Request body: {"sql":"SELECT CONCAT("p"."firstname", ' ', "p"."middlename", ' ', "p"."lastname") AS "customer_name", ROUND(SUM("so"."totaldue"), 2) AS "total_sales_value" FROM "sales_salesorderheader" AS "so" JOIN "sales_customer" AS "c" ON "so"."customerid" = "c"."customerid" JOIN "person" AS "p" ON "c"."customerid" = "p"."customerid" WHERE CAST("so"."orderdate" AS TIMESTAMP WITH TIME ZONE) >= CAST('2024-01-01 00:00:00' AS TIMESTAMP WITH TIME ZONE) AND CAST("so"."orderdate" AS TIMESTAMP WITH TIME ZONE) < CAST('2025-01-01 00:00:00' AS TIMESTAMP WITH TIME ZONE) GROUP BY 1 ORDER BY "total_sales_value" DESC"

Kindly let us know, if you need any other details.

UMANET123 avatar May 16 '25 07:05 UMANET123