Skip to content

Add missing AISQL function wrappers: ai_count_tokens, ai_multi_embed, ai_redact#4261

Open
sfc-gh-yixie wants to merge 17 commits into
mainfrom
feature/aisql-function-wrappers
Open

Add missing AISQL function wrappers: ai_count_tokens, ai_multi_embed, ai_redact#4261
sfc-gh-yixie wants to merge 17 commits into
mainfrom
feature/aisql-function-wrappers

Conversation

@sfc-gh-yixie

@sfc-gh-yixie sfc-gh-yixie commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator
  1. Which Jira issue is this PR addressing? Make sure that there is an accompanying issue to your PR.

    Fixes NO-SNOW

  2. Fill out the following pre-review checklist:

    • I am adding a new automated test(s) to verify correctness of my new code
      • If this test skips Local Testing mode, I'm requesting review from @snowflakedb/local-testing
    • I am adding new logging messages
    • I am adding a new telemetry message
    • I am adding new credentials
    • I am adding a new dependency
    • If this is a new feature/behavior, I'm adding the Local Testing parity changes.
    • I acknowledge that I have ensured my changes to be thread-safe. Follow the link for more information: Thread-safe Developer Guidelines
    • If adding any arguments to public Snowpark APIs or creating new public Snowpark APIs, I acknowledge that I have ensured my changes include AST support. Follow the link for more information: AST Support Guidelines
  3. Please describe how your code solves the related issue.

Summary

  • Added three missing AISQL function wrappers to snowflake.snowpark.functions: ai_count_tokens, ai_multi_embed, and ai_redact
  • Added corresponding DataFrame.ai.multi_embed and DataFrame.ai.redact high-level DataFrame methods
  • Fixed DataFrame.ai.count_tokens to call the new AI_COUNT_TOKENS SQL function instead of the deprecated SNOWFLAKE.CORTEX.COUNT_TOKENS
  • Added DataframeAiMultiEmbed and DataframeAiRedact proto messages to ast.proto and regenerated ast_pb2.py

Motivation

Snowflake now exposes 15 AI_* functions. Prior to this PR, the Snowpark Python client was missing three of them:

Function Status
AI_COUNT_TOKENS Missing standalone wrapper; DataFrame method used deprecated API
AI_MULTI_EMBED Missing entirely
AI_REDACT Missing entirely

Test plan

  • test_ai_count_tokens_basic — verifies ai_count_tokens returns a positive integer
  • test_ai_count_tokens_no_model — verifies functions that don't require a model
  • test_ai_count_tokens_column_input — verifies column references and relative token counts
  • test_ai_redact_basic — verifies PII placeholders appear in output
  • test_ai_redact_with_categories — verifies category-scoped redaction
  • test_ai_redact_detect_mode — verifies span metadata returned in detect mode
  • test_ai_redact_column_input — verifies column references
  • test_dataframe_ai_redact_basic / _detect_mode / _with_categories / _default_output_column
  • test_ai_multi_embed_basic — verifies embedding object structure
  • test_dataframe_ai_multi_embed_basic / _default_output_column

🤖 Generated with Claude Code

…ti_embed, ai_redact)

- Add standalone `ai_count_tokens`, `ai_multi_embed`, and `ai_redact` to `functions.py`
- Add `DataFrame.ai.multi_embed` and `DataFrame.ai.redact` DataFrame methods
- Fix `DataFrame.ai.count_tokens` to use new `AI_COUNT_TOKENS` instead of deprecated `SNOWFLAKE.CORTEX.COUNT_TOKENS`
- Add `DataframeAiMultiEmbed` and `DataframeAiRedact` proto messages; regenerate `ast_pb2.py`
- Add integration tests for all new functions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread CHANGELOG.md
Comment on lines +7 to +18
#### New Features

- Added `ai_count_tokens` function to `snowflake.snowpark.functions` to estimate token counts for AI function calls. This is the updated replacement for the deprecated `SNOWFLAKE.CORTEX.COUNT_TOKENS`.
- Added `ai_multi_embed` function to `snowflake.snowpark.functions` to generate multimodal embeddings from text, images, audio, or video files.
- Added `ai_redact` function to `snowflake.snowpark.functions` to detect and redact personally identifiable information (PII) from text.
- Added `DataFrame.ai.multi_embed` method to generate multimodal embeddings via the DataFrame API.
- Added `DataFrame.ai.redact` method to detect and redact PII from text columns via the DataFrame API.

#### Bug Fixes

- Fixed `DataFrame.ai.count_tokens` to use `AI_COUNT_TOKENS` instead of the deprecated `SNOWFLAKE.CORTEX.COUNT_TOKENS`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this shall be moved to 1.54.0 after rebasing the main

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the latest version

df._ast_id = stmt.uid
return df

@experimental(version="1.52.0")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need experimental flag -- are these functions in pupr?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@sfc-gh-aling

Copy link
Copy Markdown
Collaborator

@sfc-gh-yixie

Copy link
Copy Markdown
Collaborator Author

@codecov-commenter

codecov-commenter commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 16.81034% with 193 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.59%. Comparing base (361cd43) to head (1c8d73d).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/snowflake/snowpark/functions.py 21.19% 97 Missing and 22 partials ⚠️
src/snowflake/snowpark/dataframe_ai_functions.py 8.64% 69 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4261       +/-   ##
===========================================
- Coverage   95.21%   84.59%   -10.62%     
===========================================
  Files         171      170        -1     
  Lines       44538    44623       +85     
  Branches     7617     7674       +57     
===========================================
- Hits        42406    37748     -4658     
- Misses       1320     5646     +4326     
- Partials      812     1229      +417     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants