This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.38k
stars
2.72k
forks
source link
'NoneType' object has no attribute '__aenter__' when using cosmos client. It works for the initial calls but then it starts failing and returning this error #35947
I am using Cosmos database in my code and i am able to save some info in a container I want sometimes, other times it fails.
I call the method submit feedback where i use my cosmos client with "async with" and it seems like for some calls the client gets closed and has the value None and then i get this error when i try to save my feedback.
I saw this issue in other thread "session of AioHttpTransport is None at runtime, despite what's ruled out from typing #32309" but I did not found the solution. Please don't redirect me to that one.
I will provide some parts of my code
from azure.cosmos.aio import CosmosClient
from azure.cosmos import exceptions as cosmos_exceptions
from approaches.approach import Approach
from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach
from approaches.chatreadretrievereadvision import ChatReadRetrieveReadVisionApproach
from approaches.retrievethenread import RetrieveThenReadApproach
from approaches.retrievethenreadvision import RetrieveThenReadVisionApproach
from config import (
CONFIG_ASK_APPROACH,
CONFIG_ASK_VISION_APPROACH,
CONFIG_AUTH_CLIENT,
CONFIG_BLOB_CONTAINER_CLIENT,
CONFIG_CHAT_APPROACH,
CONFIG_CHAT_VISION_APPROACH,
CONFIG_GPT4V_DEPLOYED,
CONFIG_INGESTER,
CONFIG_OPENAI_CLIENT,
CONFIG_SEARCH_CLIENT,
CONFIG_SEMANTIC_RANKER_DEPLOYED,
CONFIG_USER_BLOB_CONTAINER_CLIENT,
CONFIG_USER_UPLOAD_ENABLED,
CONFIG_VECTOR_SEARCH_ENABLED,
CONFIG_COSMOS_CLIENT,
)
CONFIG_COSMOS_CLIENT = "cosmos_client"
...
@bp.route("/submit-feedback", methods=["POST"])
async def submit_feedback():
try:
if not request.is_json:
return jsonify({"error": "Request must be JSON"}), 415
request_json = await request.get_json()
# Validate required fields
feedback_text = request_json.get("feedbackText")
Liked = request_json.get("Like_or_Dislike")
question = request_json.get("question")
answerContent = request_json.get("answerContent")
citations = request_json.get("citations")
print(feedback_text,Liked,question,answerContent)
logging.info("Feedback text: %s", feedback_text)
logging.info("Liked: %s", Liked)
logging.info("Question: %s", question)
logging.info("Answer content: %s", answerContent)
if not feedback_text:
return jsonify({"error": "Missing required fields: feedbackText"}), 400
if not Liked:
return jsonify({"error": "Missing required field: isLiked"}), 400
if not question:
return jsonify({"error": "Missing required field: question"}), 400
if not answerContent:
return jsonify({"error": "Missing required field: answerContent"}), 400
# Get the Cosmos DB client from the app configuration
cosmos_client = current_app.config[CONFIG_COSMOS_CLIENT]
if cosmos_client is None:
logging.error("Cosmos DB client is not configured")
return jsonify({"error": "Internal server error cosmos client"}), 500
AZURE_COSMOSDB_DATABASE = os.getenv("AZURE_COSMOSDB_DATABASE")
AZURE_COSMOSDB_CONVERSATIONS_CONTAINER = os.getenv("AZURE_COSMOSDB_CONVERSATIONS_CONTAINER")
if not AZURE_COSMOSDB_DATABASE:
logging.error("AZURE_COSMOSDB_DATABASE environment variable is not set")
return jsonify({"error": "Internal server error"}), 500
if not AZURE_COSMOSDB_CONVERSATIONS_CONTAINER:
logging.error("AZURE_COSMOSDB_CONVERSATIONS_CONTAINER environment variable is not set")
return jsonify({"error": "Internal server error"}), 500
async with cosmos_client:
database = cosmos_client.get_database_client(AZURE_COSMOSDB_DATABASE)
container = database.get_container_client(AZURE_COSMOSDB_CONVERSATIONS_CONTAINER)
# Create a feedback document
feedback_document = {
"id": str(uuid.uuid4()),
"feedback": Liked,
"feedbackText": feedback_text,
"question": question,
"answerContent": answerContent,
"citations": citations,
"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
}
try:
await container.upsert_item(feedback_document)
except Exception as e:
logging.error(f"Error upserting feedback document: {e}")
logging.error(traceback.format_exc())
return jsonify({"error": f"Internal server error: {e}"}), 500
return jsonify({"success": True, "message": "Feedback successfully recorded!"})
except Exception as e:
logging.error(f"Error in /submit-feedback: {e}")
logging.error(traceback.format_exc())
return jsonify({"error": str(e)}), 500
I am using Cosmos database in my code and i am able to save some info in a container I want sometimes, other times it fails. I call the method submit feedback where i use my cosmos client with "async with" and it seems like for some calls the client gets closed and has the value None and then i get this error when i try to save my feedback.
I saw this issue in other thread "session of AioHttpTransport is None at runtime, despite what's ruled out from typing #32309" but I did not found the solution. Please don't redirect me to that one.
I will provide some parts of my code
from azure.cosmos.aio import CosmosClient from azure.cosmos import exceptions as cosmos_exceptions
from approaches.approach import Approach from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach from approaches.chatreadretrievereadvision import ChatReadRetrieveReadVisionApproach from approaches.retrievethenread import RetrieveThenReadApproach from approaches.retrievethenreadvision import RetrieveThenReadVisionApproach from config import ( CONFIG_ASK_APPROACH, CONFIG_ASK_VISION_APPROACH, CONFIG_AUTH_CLIENT, CONFIG_BLOB_CONTAINER_CLIENT, CONFIG_CHAT_APPROACH, CONFIG_CHAT_VISION_APPROACH, CONFIG_GPT4V_DEPLOYED, CONFIG_INGESTER, CONFIG_OPENAI_CLIENT, CONFIG_SEARCH_CLIENT, CONFIG_SEMANTIC_RANKER_DEPLOYED, CONFIG_USER_BLOB_CONTAINER_CLIENT, CONFIG_USER_UPLOAD_ENABLED, CONFIG_VECTOR_SEARCH_ENABLED,
CONFIG_COSMOS_CLIENT,
)
CONFIG_COSMOS_CLIENT = "cosmos_client"
...
@bp.route("/submit-feedback", methods=["POST"]) async def submit_feedback(): try: if not request.is_json: return jsonify({"error": "Request must be JSON"}), 415
...
@bp.before_app_serving async def setup_clients():
cosmos_client = CosmosClient( url=f"https://{AZURE_COSMOSDB_ACCOUNT}.documents.azure.com:443/", credential=AZURE_COSMOSDB_ACCOUNT_KEY ) print(f"Cosmos client: {cosmos_client}")
current_app.config[CONFIG_COSMOS_CLIENT] = cosmos_client
These are parts of the backend (app.py) of my flask app
Thanks !