Skip to content

feat: allow caller-supplied Snowpark session in ChatSnowflake - #428

Merged
cpsievert merged 2 commits into
chat-close-lifecyclefrom
snowflake-session-param
Sep 4, 2026
Merged

feat: allow caller-supplied Snowpark session in ChatSnowflake#428
cpsievert merged 2 commits into
chat-close-lifecyclefrom
snowflake-session-param

Conversation

@cpsievert

Copy link
Copy Markdown
Collaborator

Stacked on #427.

Summary

ChatSnowflake() gains a session parameter accepting a caller-supplied snowflake.snowpark.Session, mirroring the workspace_client pattern in ChatDatabricks(). When a session is supplied, the provider uses it directly and records _owns_session = False, so Chat.close() leaves it alone — only sessions created by chatlas itself are closed. This lets users share one Snowpark session across multiple chats (e.g., per-session chats in a Shiny app) without close() tearing down the shared connection.

Verification

from snowflake.snowpark import Session
from chatlas import ChatSnowflake

session = Session.builder.configs({"connection_name": "my_connection"}).create()
chat1 = ChatSnowflake(model="claude-sonnet-4-6", session=session)
chat2 = ChatSnowflake(model="claude-sonnet-4-6", session=session)
chat1.close()  # does NOT close the shared session
chat2.close()  # still fine
session.close()  # caller closes it when done

Tests: test_snowflake_provider_close_ownership in tests/test_close.py verifies a supplied session is never closed; existing close tests confirm chatlas-created sessions still close (idempotently).

Mirrors the ChatDatabricks workspace_client pattern: when a session is
provided, the provider uses it directly and close() leaves it alone;
only sessions created by chatlas itself are closed.
@cpsievert
cpsievert marked this pull request as ready for review September 4, 2026 15:35
@cpsievert
cpsievert merged commit 46b1331 into chat-close-lifecycle Sep 4, 2026
12 checks passed
@cpsievert
cpsievert deleted the snowflake-session-param branch September 4, 2026 16:10
cpsievert added a commit that referenced this pull request Sep 4, 2026
* feat: add Chat.close()/close_async() lifecycle API

Chat gains close() and close_async() methods (plus context-manager
support) for releasing resources held by the provider: HTTP connection
pools, the Snowflake Snowpark session/connection, and (via
close_async()) MCP server sessions. This supports long-lived
applications like Shiny that create a chat per user session, e.g.
session.on_ended(chat.close).

- Provider ABC gains no-op close()/close_async() with documented
  ownership (only provider-created resources are closed) and
  idempotency rules
- Overrides on the OpenAI generic base, Anthropic, Bedrock Converse,
  Google, and Snowflake providers; all other providers inherit
- SnowflakeProvider now retains its Snowpark session so it can be
  closed
- Databricks needs no override (caller-supplied WorkspaceClient is
  never retained); ownership behavior pinned by test

* Address close() lifecycle review feedback

- Bump google-genai minimum to >=2.0.0 (Client.close()/aio.aclose()
  were both added in 2.0.0) and drop the getattr guard.
- Document that ChatDatabricks close() leaves a caller-supplied
  WorkspaceClient open.
- Warn in Chat.close() when MCP server sessions are still open.

* Remove unused typing.cast import (fixes ruff F401)

* feat: allow caller-supplied Snowpark session in ChatSnowflake (#428)

* feat: allow caller-supplied Snowpark session in ChatSnowflake

Mirrors the ChatDatabricks workspace_client pattern: when a session is
provided, the provider uses it directly and close() leaves it alone;
only sessions created by chatlas itself are closed.

* docs: changelog entry for ChatSnowflake session parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant