fix this bug: {"asctime": "2023-12-13 01:28:23,141", "name":…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
fix this bug: {"asctime": "2023-12-13 01:28:23,141", "name": "sweetspot.server_v2.server_v2.core.controller", "levelname": "ERROR", "message": "Traceback (most recent call last):\n File "/app/sweetspot/server_v2/server_v2/v1/controllers/pursuits.py", line 71, in post\n FederalGrantPursuit.from_orm(pursuit.federal_grant_pursuit) if pursuit.federal_grant_pursuit else None\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/attributes.py", line 566, in get\n return self.impl.get(state, dict_) # type: ignore[no-any-return]\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get\n value = self._fire_loader_callables(state, key, passive)\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/attributes.py", line 1121, in fire_loader_callables\n return self.callable(state, passive)\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/strategies.py", line 967, in _load_for_state\n return self._emit_lazyload(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/strategies.py", line 1130, in _emit_lazyload\n result = session.execute(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2308, in execute\n return self._execute_internal(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2190, in _execute_internal\n result: Result[Any] = compile_state_cls.orm_execute_statement(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/context.py", line 293, in orm_execute_statement\n result = conn.execute(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1416, in execute\n return meth(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 516, in _execute_on_connection\n return connection._execute_clauseelement(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1639, in _execute_clauseelement\n ret = self._execute_context(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1848, in _execute_context\n return self._exec_single_context(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1988, in _exec_single_context\n self._handle_dbapi_exception(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2346, in _handle_dbapi_exception\n raise exc_info[1].with_traceback(exc_info[2])\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1969, in _exec_single_context\n self.dialect.do_execute(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 922, in do_execute\n cursor.execute(statement, parameters)\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 591, in execute\n self.adapt_connection.await(\n File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 116, in await_only\n raise exc.MissingGreenlet(\nsqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)\n"} this is my pursuit: import datetime import uuid from enum import Enum from typing import TYPE_CHECKING, List, Optional
from sqlalchemy import UUID, Column, DateTime from sqlalchemy import Enum as SQLEnum from sqlalchemy import ForeignKey, Table, text from sqlalchemy.orm import Mapped, mapped_column, relationship
if TYPE_CHECKING: from sweetspot.db.db.pg.models.document import Document from sweetspot.db.db.pg.models.user import User
from sweetspot.db.db.base import PostgresBase
pursuit_to_user = Table( "pursuit_to_user", PostgresBase.metadata, Column("id", UUID, primary_key=True, server_default=text("gen_random_uuid()")), Column("created_at", DateTime(True), nullable=False, server_default=text("now()")), Column("pursuit_id", ForeignKey("pursuits.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False), Column("user_id", ForeignKey("users.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False), )
class PursuitStatus(str, Enum): NOT_STARTED = "not_started" IN_PROGRESS = "in_progress" IN_REVIEW = "in_review" AWAITING_AWARD = "awaiting_award" COMPLETED = "completed"
class PursuitType(str, Enum): FEDERAL_GRANT_PURSUIT = "federal_grant_pursuit" FEDERAL_CONTRACT_PURSUIT = "federal_contract_pursuit"
class FederalGrantPursuit(PostgresBase): tablename = "federal_grant_pursuits" entity_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("federal_grants.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False ) pursuit_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("pursuits.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False )
class FederalContractPursuit(PostgresBase): tablename = "federal_contract_pursuits" entity_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("federal_contracts.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False ) pursuit_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("pursuits.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False )
class Pursuit(PostgresBase): tablename = "pursuits"
organization_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("organizations.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False
)
status: Mapped[PursuitStatus] = mapped_column(SQLEnum(PursuitStatus, name="pursuit_status"), nullable=False)
type: Mapped[PursuitType] = mapped_column(SQLEnum(PursuitType, name="pursuit_type"), nullable=False)
title: Mapped[str] = mapped_column(nullable=False)
due_date: Mapped[datetime.datetime] = mapped_column(DateTime(timezone=True), nullable=False)
federal_contract_pursuit: Mapped[Optional[FederalContractPursuit]] = relationship(
"FederalContractPursuit", backref="pursuit", uselist=False, lazy="selectin"
)
federal_grant_pursuit: Mapped[Optional[FederalGrantPursuit]] = relationship(
"FederalGrantPursuit", backref="pursuit", uselist=False, lazy="selectin"
)
documents: Mapped[List["Document"]] = relationship("Document", backref="pursuit")
users: Mapped[List["User"]] = relationship("User", secondary=pursuit_to_user, backref="pursuits")
The error you're facing is related to using SQLAlchemy with asyncpg in a synchronous context. The error message "greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?" indicates that you're trying to perform an asynchronous operation in a synchronous function.
To fix this bug, you need to make sure you're using the asynchronous context when using asyncpg with SQLAlchemy. Here's a simple example of how to use asyncpg properly with SQLAlchemy:
pip install sqlalchemy asyncpg sqlalchemy_aio
import asyncio
from sqlalchemy import create_engine, select
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.orm.session import AsyncSession
Base = declarative_base()
# Your models go here.
# Replace the connection string with your actual PostgreSQL connection string.
DATABASE_URL = "postgresql+asyncpg://user:password@localhost/dbname"
async def main():
# Create an async engine.
engine = create_async_engine(DATABASE_URL, future=True)
# Create an async session maker.
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
# Create an async session and perform an async query.
async with async_session() as session:
# Replace "YourModel" with one of your actual model classes.
result = await session.execute(select(YourModel))
data = result.scalars().all()
print(data)
await engine.dispose()
# Run the async main function using asyncio.
asyncio.run(main())
In the code you provided, you need to modify the part where you are querying the database to use the async context properly. You need to update the pursuits.py file's post function and convert it to async.
Please note that without the complete code, especially the relevant controller part, it is difficult to provide an accurate solution. If you can provide the related code, I can give you a more specific solution.