Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

[BUG] Upsert suggestion endpoint not working in DEV #3423

Closed
gabrielmbmb opened this issue Jul 17, 2023 · 0 comments
Closed

[BUG] Upsert suggestion endpoint not working in DEV #3423

gabrielmbmb opened this issue Jul 17, 2023 · 0 comments
Assignees
Labels
area: api Indicates that an issue or pull request is related to the Fast API server or REST endpoints type: bug Indicates an unexpected problem or unintended behavior
Milestone

Comments

@gabrielmbmb
Copy link
Member

Describe the bug

@nataliaElv and @alvarobartt reported that the new PUT /api/v1/records/{record_id}/suggestions added in #3391 is not working properly in the internal development server.

After some investigation, the error is being caused when trying to execute the upsert query that the endpoint use to create/update the suggestion and the sqlite3 version (3.34.1) that comes with the argilla-server Docker image. The upsert query uses the RETURNING clause which its support for using with DELETE, INSERT or UPDATE was added in the sqlite3>=3.35.0 version.

image

Stacktrace and Code to create the bug

Error traceback
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1965, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 921, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 113, in execute
    self._adapt_connection._handle_exception(error)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 95, in execute
    raise error
    response = await func(request)
    self.await_(_cursor.execute(operation, parameters))
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 125, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 185, in greenlet_spawn
    value = await result
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/cursor.py", line 48, in execute
    await self._execute(self._cursor.execute, sql, parameters)
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/cursor.py", line 40, in _execute
    return await self._conn._execute(fn, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/core.py", line 133, in _execute
    return await future
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/core.py", line 106, in run
    result = function()
The above exception was the direct cause of the following exception:

    result = context.throw(*sys.exc_info())

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1984, in _exec_single_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 706, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 113, in execute
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 443, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 185, in greenlet_spawn
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/brotli_asgi/__init__.py", line 81, in __call__
    await responder(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/brotli_asgi/__init__.py", line 125, in __call__
    await self.app(scope, receive, self.send_with_brotli)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 161, in run_endpoint_function
    return await dependant.call(**values)
  File "/usr/local/lib/python3.9/site-packages/argilla/server/apis/v1/handlers/records.py", line 129, in upsert_suggestion
    return await datasets.upsert_suggestion(db, record, question, suggestion_create)
  File "/usr/local/lib/python3.9/site-packages/argilla/server/contexts/datasets.py", line 495, in upsert_suggestion
    return await Suggestion.upsert(
  File "/usr/local/lib/python3.9/site-packages/argilla/server/models/mixins.py", line 122, in upsert
    upserted = await cls.upsert_many(db, [schema], constraints, autocommit)
  File "/usr/local/lib/python3.9/site-packages/argilla/server/models/mixins.py", line 108, in upsert_many
    result = await db.execute(upsert_stmt)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/ext/asyncio/session.py", line 439, in execute
    result = await greenlet_spawn(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 190, in greenlet_spawn
    return self._execute_internal(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2246, in execute
    return self._execute_internal(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2141, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/bulk_persistence.py", line 1272, in orm_execute_statement
    result = conn.execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1412, in execute
    return meth(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 483, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1635, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1844, in _execute_context
    return self._exec_single_context(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2339, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2339, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1965, in _exec_single_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 921, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 263, in _handle_exception
    self._adapt_connection._handle_exception(error)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 263, in _handle_exception
    raise error
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/sqlite/aiosqlite.py", line 95, in execute
    self.await_(_cursor.execute(operation, parameters))
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 125, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/cursor.py", line 48, in execute
    value = await result
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/cursor.py", line 48, in execute
    await self._execute(self._cursor.execute, sql, parameters)
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/cursor.py", line 40, in _execute
    return await self._conn._execute(fn, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/core.py", line 133, in _execute
    return await future
  File "/usr/local/lib/python3.9/site-packages/aiosqlite/core.py", line 106, in run
    result = function()
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "RETURNING": syntax error
[SQL: INSERT INTO suggestions (value, score, agent, type, record_id, question_id, id, inserted_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) ON CONFLICT (record_id, question_id) DO UPDATE SET value = excluded.value, score = excluded.score, agent = excluded.agent, type = excluded.type, updated_at = CURRENT_TIMESTAMP RETURNING value, score, agent, type, record_id, question_id, id, inserted_at, updated_at]
[parameters: ('"negative"', 0.75, 'string', 'model', '832968839b07492ab285b525651c1730', 'f947a63edb3046eba4b33f13475fb7c7', '6c08bb3da7b64e97bdcec779b2ebfb3c')]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

Expected behavior
The endpoint works and creates or updates a suggestion

Environment:

  • Argilla Version [e.g. 1.0.0]: 1.13.0
  • ElasticSearch Version [e.g. 7.10.2]:
  • Docker Image (optional) [e.g. argilla:v1.0.0]: argilla/argilla-server:releases-1.13.0
@gabrielmbmb gabrielmbmb added type: bug Indicates an unexpected problem or unintended behavior area: api Indicates that an issue or pull request is related to the Fast API server or REST endpoints labels Jul 17, 2023
@gabrielmbmb gabrielmbmb added this to the v1.13.0 milestone Jul 17, 2023
@gabrielmbmb gabrielmbmb self-assigned this Jul 17, 2023
frascuchon pushed a commit that referenced this issue Jul 18, 2023
# Description

This PRs updates the `Dockerfile` parent image from `python:3.9.16-slim`
to `python:3.10.12-slim`, which comes with `sqlite3==3.40.1` installed.

In addition, the `quickstart.Dockerfile` has been updated to:

- Use `argilla/argilla-server:latest` as parent image
- Install `elasticsearch` as described in
https://www.elastic.co/guide/en/elasticsearch/reference/current/deb.html#deb

This way, the `Dockerfile` and `quickstart.Dockerfile` are aligned using
the same system dependencies, python dependencies and Python version.

Finally, a new workflow has been added `build-push-docker.yml` which
builds and push the Docker image and has the version of the build docker
image as output. It's been needed to add this, as for building the
`argilla/argilla-quickstart` we need to pass the build argument
`ARGILLA_VERSION` with the desired value of the
`argilla/argilla-server`.

Closes #3423

**Type of change**

- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [x] Improvement (change adding some improvement to an existing
functionality)

**How Has This Been Tested**

- [x] `pre.argilla.io`
- [x] HF Space
- [x] HF Space with `1.12.0` upgrade to `1.13.0`. The data is not lost.

**Checklist**

- [ ] I added relevant documentation
- [x] follows the style guidelines of this project
- [x] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [x] I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Indicates that an issue or pull request is related to the Fast API server or REST endpoints type: bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants