Repository: truera/trulens
Issue: [BUG]SQL Execution Canceled During INSERT Operation in Snowflake
Root cause: External Dependency Breakage

**Bug Description**
What happened?
An unexpected error occurs while attempting to execute an INSERT operation in Snowflake using snowflake-connector. The transaction is canceled with a ProgrammingError, citing that the transaction is being committed, and SQL execution is canceled.


**To Reproduce**
Which steps should someone take to run into the same error? A small, reproducible code example is useful here.
Unfortunately, I cannot reliably reproduce the error myself as it happens intermittently. However, it occurs quite often during execution.

**Expected behavior**
A clear and concise description of what you expected to happen.
The data should be successfully saved into Snowflake without any errors.

**Relevant Logs/Tracebacks**
Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks. If the issue is related to the TruLens dashboard, please also include a screenshot.


`un unexpected error occurred: (snowflake.connector.errors.ProgrammingError) 000614 (57014): 01b99a49-0512-a1e0-0074-820306b2185a: Transaction '5c518634-c4c0-471c-8d8a-bd20b4dd453e', id '1736452409002000000', was committed, SQL execution canceled.
[SQL: INSERT INTO trulens_records (record_id, app_id, input, output, record_json, tags, ts, cost_json, perf_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)]

(Background on this error at: https://sqlalche.me/e/20/f405)`


**Environment:**

- OS: [e.g. MacOS, Windows]
- Python Version
- TruLens version
- Versions of other relevant installed libraries

**Additional context**
Add any other context about the problem here.

`def initialize_sessions(sf_settings: dict, password: str) -> Tuple[Session, TruSession]:
    """
    Initialize Snowpark session and TruLens session.
    """
    sf_config = sf_settings.copy()
    sf_config.update({
        "connection_timeout": 300,  # 5 minutes
        "client_session_keep_alive": True,
        "session_timeout": 600  # 10 minutes
    })
    
    # Create Snowpark session
    snowpark_session = Session.builder.configs(sf_settings).create()
    print("Snowpark session created successfully.")

    # Initialize SnowflakeConnector for TruLens
    conn = SnowflakeConnector(
        snowpark_session=snowpark_session,
        password=password,
        database_redact_keys=True
    )

    # Create TruSession
    session = TruSession(connector=conn)
    # session.reset_database()
    print("TruSession initialized and database reset.")
    return snowpark_session, session

with tru_app:
                response = retrieval_app.ask_question(
                    query=qa.get("question"),
                )
                print(f"Response: {response}")
`


Hey @HannaHUp - are you using SQL to call Snowflake Cortex in your app? That may be the cause here. Can you change it to use the python API?

> are you using SQL to call Snowflake Cortex in your app? That may be the cause here. Can you change it to use the python API?

Thank you for your response! I believe I am already using the Python API to interact with Snowflake. Specifically, I am using the SnowflakeConnector class from trulens.connectors.snowflake

`
snowpark_session = Session.builder.configs(sf_settings).create()
    print("Snowpark session created successfully.")

    conn = SnowflakeConnector(
        snowpark_session=snowpark_session,
        password=password,
        database_redact_keys=True
    )

    session = TruSession(connector=conn)
    return snowpark_session, session
`

can you share your app code?

[trulens_sf.zip](https://github.com/user-attachments/files/18367100/trulens_sf.zip)
@sfc-gh-jreini 
Here it is.
Thank you

https://github.com/truera/trulens/issues/1717
I also have this NaN issue from the same code.


Looking at your code, feel like I'm missing where your app is actually defined. Is there additional code where you set up `ConversationalRetrievalAgent`? Or is that imported (like from langchain? the import for that isn't in the .py)

[trulens_sf.zip](https://github.com/user-attachments/files/18367312/trulens_sf.zip)
@sfc-gh-jreini 
Sorry. Here is the additional code where I set up ConversationalRetrievalAgent.

One possible issue here is that the output of `ask_question` is returned in a different order depending on if `expected_output` is included as an arg.

This is because `on_output()` feedback selector is selecting the first output of your function. But in the cases where `ask_question` doesn't get an expected_output, the first output of the function is `context_docs` instead of `generated_message`.

I'd recommend switching the order of the method output so that generated_message is always the first value returned in the Tuple. So in L100, change `return context_docs, generated_message` to `return generated_message, context_docs`

Hi,
Thank you for the suggestion. We define a list of questions and answers in qa_list, so we always expect consistent output for this test case. However, we’ve observed inconsistent results—sometimes we get the expected result, and other times we don’t.

We’ll also modify the order of the method output.

Good to know. Let me know if changing the order of the method output helps or if you're still seeing issues - thanks!

Hey @HannaHUp - we discovered some incompatibility with snowflake-sqlalchemy v1.7.2 that may also be causing this issue. Can you test with snowflake-sqlalchemy==1.7.1 or earlier? Thanks!

@sfc-gh-jreini
The issue is fixed by downgrading snowflake-sqlalchemy to 1.7.1!

@HannaHUp 
we have a pending PR to resolve this incompatibility in the latest snowflake-sqlalchemy https://github.com/truera/trulens/pull/1719

I also have an issue with my TRULENS_RECORDS table. Some dictionaries in the RECORD_JSON column have the 0 values:

> {"n_requests": 0, "n_successful_requests": 0, "n_completion_requests": 0, "n_classification_requests": 0, "n_classes": 0, "n_embedding_requests": 0, "n_embeddings": 0, "n_tokens": 0, "n_stream_chunks": 0, "n_prompt_tokens": 0, "n_completion_tokens": 0, "n_cortex_guardrails_tokens": 0, "cost": 0.0, "cost_currency": "USD"}

Similarly, the COST_JSON column also contains all zeros:

> {"n_requests": 0, "n_successful_requests": 0, "n_completion_requests": 0, "n_classification_requests": 0, "n_classes": 0, "n_embedding_requests": 0, "n_embeddings": 0, "n_tokens": 0, "n_stream_chunks": 0, "n_prompt_tokens": 0, "n_completion_tokens": 0, "n_cortex_guardrails_tokens": 0, "cost": 0.0, "cost_currency": "USD"}

Could this issue be related to Snowflake package problem?
![image](https://github.com/user-attachments/assets/a09dbe0f-6837-4a96-acd4-e31611eb9fb5)


Looks like original issue was resolved and second issue is in another github issue (and also resolved). closing