Skip to content

Bug: Litestar 2.8.2 + SQLSpecPlugin returns GeneratorContextManager instead of SqliteDriver (AttributeError: '_GeneratorContextManager' object has no attribute 'cursor')_ #238

@sion908

Description

@sion908

Description

Summary

  • After porting the implementation from sqlspec/docs/examples/frameworks/litestar/sqlite_app.py into the local litestar-sqlspec-app project, the application fails to resolve the injected SQLite session and raises AttributeError: '_GeneratorContextManager' object has no attribute 'cursor'.
  • Although uv.lock within the sqlspec repo references Litestar 2.18.0, the container resolves Litestar 2.8.2, so version mismatches may be involved.

Environment

  • Project: litestar-sqlspec-app (Docker / Poetry)
  • Host OS: macOS
  • Python: 3.12 (inside container)
  • sqlspec: 0.29.0
  • Litestar: 2.8.2 (confirmed with poetry run python -c "import litestar; print(litestar.__version__)")
  • Docker Compose: Using the provided docker-compose.yml

Main Packages

Package Version
Python 3.12
litestar 2.8.2
sqlspec 0.29.0
pydantic 2.x
python-dotenv 1.2.x
pytest 8.4.x

Steps to Reproduce

  1. Replicate sqlspec/docs/examples/frameworks/litestar/sqlite_app.py into app/lite.py:
    • use SQLSpecPlugin
    • call config.provide_session() in seed_database to create tables and seed demo data
  2. Run docker-compose up --build
  3. Access http://localhost:8000/articles

Actual Behavior

A request triggers the following exception and returns HTTP 500:

AttributeError: '_GeneratorContextManager' object has no attribute 'cursor'

Excerpt from the full traceback:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/sqlspec/driver/_sync.py", line 64, in dispatch_statement_execution
    with self.handle_database_exceptions(), self.with_cursor(connection) as cursor:
  File "/app/.venv/lib/python3.12/site-packages/sqlspec/adapters/sqlite/driver.py", line 85, in __enter__
    self.cursor = self.connection.cursor()
AttributeError: '_GeneratorContextManager' object has no attribute 'cursor'

During handling of the above exception, another exception occurred:
...
  File "/app/.venv/lib/python3.12/site-packages/sqlspec/extensions/litestar/handlers.py", line 66, in handler
    await ensure_async_(connection.close)()
AttributeError: '_GeneratorContextManager' object has no attribute 'close'

Expected Behavior

  • Litestar’s DI should provide a valid SqliteDriver, and GET /articles should return a list of articles with 200 OK.
  • Behavior should match the official example.

Additional Context

  • The official example keeps the synchronous code (with config.provide_session() as session:); I tried making seed_database async and using async with, but the underlying error persists.
  • It appears sqlspec/extensions/litestar/plugin.py expects Litestar 2.18.0+, while the current environment resolves 2.8.2, which may cause DI behavior differences.

Willingness to Contribute / Needed Guidance

  • If this is already known, I’m willing to work on a fix. Any guidance on where to look (e.g., Litestar DI changes, expected return values from provide_session) would be greatly appreciated.
  • If there are reference materials or test cases that would help with debugging, please share them.

URL to code causing the issue

No response

MCVE

# Your MCVE code here

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

Package Version

Package Version
Python 3.12
litestar 2.8.2
sqlspec 0.29.0
pydantic 2.x
python-dotenv 1.2.x
pytest 8.4.x

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions