Skip to content

Conversation

@smamindl
Copy link

@smamindl smamindl commented Dec 9, 2025

Related Issues/PRs

Related to : 2444

#xxx

What changes are proposed in this pull request?

This PR adds null-safety checks to the transform method in OpenAIEmbedding to handle cases where the OpenAI API response may contain null data.

How is this patch tested?

  • I have written tests (not required for typo or doc fix) and confirmed the proposed feature/bug-fix/change works.

Does this PR change any dependencies?

  • No. You can skip this section.
  • Yes. Make sure the dependencies are resolved correctly, and list changes here.

Does this PR add a new feature? If so, have you added samples on website?

  • No. You can skip this section.
  • Yes. Make sure you have added samples following below steps.
  1. Find the corresponding markdown file for your new feature in website/docs/documentation folder.
    Make sure you choose the correct class estimators/transformers and namespace.
  2. Follow the pattern in markdown file and add another section for your new API, including pyspark, scala (and .NET potentially) samples.
  3. Make sure the DocTable points to correct API link.
  4. Navigate to website folder, and run yarn run start to make sure the website renders correctly.
  5. Don't forget to add <!--pytest-codeblocks:cont--> before each python code blocks to enable auto-tests for python samples.
  6. Make sure the WebsiteSamplesTests job pass in the pipeline.

@github-actions
Copy link

github-actions bot commented Dec 9, 2025

Hey @smamindl 👋!
Thank you so much for contributing to our repository 🙌.
Someone from SynapseML Team will be reviewing this pull request soon.

We use semantic commit messages to streamline the release process.
Before your pull request can be merged, you should make sure your first commit and PR title start with a semantic prefix.
This helps us to create release messages and credit you for your hard work!

Examples of commit messages with semantic prefixes:

  • fix: Fix LightGBM crashes with empty partitions
  • feat: Make HTTP on Spark back-offs configurable
  • docs: Update Spark Serving usage
  • build: Add codecov support
  • perf: improve LightGBM memory usage
  • refactor: make python code generation rely on classes
  • style: Remove nulls from CNTKModel
  • test: Add test coverage for CNTKModel

To test your commit locally, please follow our guild on building from source.
Check out the developer guide for additional guidance on testing your change.

@smamindl smamindl changed the title Add null-safety checks in OpenAIEmbedding transform method fix: Add null-safety checks in OpenAIEmbedding transform method Dec 9, 2025
@smamindl smamindl changed the title fix: Add null-safety checks in OpenAIEmbedding transform method BugFix: Add null-safety checks in OpenAIEmbedding transform method Dec 9, 2025
@smamindl smamindl changed the title BugFix: Add null-safety checks in OpenAIEmbedding transform method fix: Add null-safety checks in OpenAIEmbedding transform method Dec 9, 2025
@smamindl smamindl requested a review from Copilot December 10, 2025 00:14
@smamindl
Copy link
Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds null-safety checks to the transform method in OpenAIEmbedding to handle cases where the OpenAI API response may contain null data. The changes introduce defensive checks using Spark's when().otherwise() pattern to prevent runtime errors when accessing the embedding data and converting it to a vector.

  • Adds necessary imports (lit and when) for null-safety checks
  • Implements null checks for the data field before accessing array elements
  • Implements null checks for the embedding array before converting to vector

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

val embeddingArrayCol = element_at(responseCol.getField("data"), 1).getField("embedding")
val vectorCol = array_to_vector(embeddingArrayCol)
val dataField = responseCol.getField("data")
val embeddingArrayCol = when(dataField.isNotNull,
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The null check on dataField.isNotNull doesn't handle the case where data is an empty array. If the API returns an empty array for data, element_at(dataField, 1) will return null, and calling .getField("embedding") on null will cause a runtime error. Consider adding an additional check for array size using size(dataField) > 0 or wrapping the element_at(...).getField("embedding") in a try-catch, or better yet, combine the checks: when(dataField.isNotNull && size(dataField) > 0, ...).

Suggested change
val embeddingArrayCol = when(dataField.isNotNull,
val embeddingArrayCol = when(dataField.isNotNull && size(dataField) > 0,

Copilot uses AI. Check for mistakes.
Comment on lines 92 to +101
override def transform(dataset: Dataset[_]): DataFrame = {
val parsed = super.transform(dataset)
val responseCol = col(getOutputCol)
val embeddingArrayCol = element_at(responseCol.getField("data"), 1).getField("embedding")
val vectorCol = array_to_vector(embeddingArrayCol)
val dataField = responseCol.getField("data")
val embeddingArrayCol = when(dataField.isNotNull,
element_at(dataField, 1).getField("embedding")
).otherwise(lit(null))
val vectorCol = when(embeddingArrayCol.isNotNull,
array_to_vector(embeddingArrayCol)
).otherwise(lit(null).cast(VectorType))
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The new null-safety logic lacks test coverage. Since the test suite in OpenAIEmbeddingsSuite.scala has comprehensive tests for other scenarios, consider adding tests that verify the behavior when the API returns null data or empty arrays. This will ensure the null-safety checks work correctly and prevent regressions.

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.37%. Comparing base (bfa8c46) to head (ab84a9b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2455      +/-   ##
==========================================
+ Coverage   84.34%   84.37%   +0.03%     
==========================================
  Files         335      335              
  Lines       17723    17728       +5     
  Branches     1616     1612       -4     
==========================================
+ Hits        14948    14958      +10     
+ Misses       2775     2770       -5     

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

import scala.language.existentials
import org.apache.spark.sql.functions.{col, element_at, struct}
import org.apache.spark.sql.functions.{col, element_at, lit, struct, when}
import spray.json.DefaultJsonProtocol._
Copy link
Collaborator

Choose a reason for hiding this comment

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

@smamindl please add a test in OpenAIEmbeddingSuite.scala and potentially in OpenAIPromptsuite.scala so this null case is caught if it happens in future

Copy link
Collaborator

@ranadeepsingh ranadeepsingh left a comment

Choose a reason for hiding this comment

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

Add tests

@BrendanWalsh
Copy link
Collaborator

Fixed in b979064

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.

5 participants