Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[C API] Implement `ov_tensor_create_from_string_array` c-api (#29982)

Rahul76b10adb23a5
diff --git a/src/bindings/c/docs/api_overview.md b/src/bindings/c/docs/api_overview.md
index e9a287c6341..29dc08dfd60 100644
--- a/src/bindings/c/docs/api_overview.md
+++ b/src/bindings/c/docs/api_overview.md
@@ -1080,6 +1080,14 @@ This struct provides an interface to infer requests of `ov_compiled_model_t` and
- `tensor` - A point to ov_tensor_t
- Return value: Status code of the operation: OK(0) for success.
+- `ov_status_e ov_tensor_create_from_string_array(const char** string_array, const size_t array_size, const ov_shape_t shape, ov_tensor_t** tensor)`
+ - Description: Constructs Tensor using string array and size.
+ - `string_array` - String array
+ - `array_size` - Number of elements in string array
+ - `shape` - Tensor shape
+ - `tensor` - A point to ov_tensor_t
+ - Return value: Status code of the operation: OK(0) for success.
+
- `ov_status_e ov_tensor_create(const ov_element_type_e type, const ov_shape_t shape, ov_tensor_t** tensor)`
- Description: Constructs Tensor using element type and shape. Allocate internal host storage using default allocator.
- Parameters:
diff --git a/src/bindings/c/include/openvino/c/ov_tensor.h b/src/bindings/c/include/openvino/c/ov_tensor.h
index 946e687a2e7..894308887a3 100644
--- a/src/bindings/c/include/openvino/c/ov_tensor.h
+++ b/src/bindings/c/include/openvino/c/ov_tensor.h
@@ -57,6 +57,21 @@ ov_tensor_create(const ov_element_type_e type, const ov_shape_t shape, ov_tensor
OPENVINO_C_API(ov_status_e)
ov_tensor_set_shape(ov_tensor_t* tensor, const ov_shape_t shape);
+/**
+ * @brief Constructs a new tensor using a string array.
+ * @ingroup ov_tensor_c_api
+ * @param string_array An array of strings
+ * @param array_size The size of the string array
+ * @param shape Tensor shape
+ * @param tensor A point to ov_tensor_t
+ * @return Status code of the operation: OK(0) for success.
+ */
+OPENVINO_C_API(ov_status_e)
+ov_tensor_create_from_string_array(const char** string_array,
+ const size_t array_size,
+ const ov_shape_t shape,
+ ov_tensor_t** tensor);
+
/**
* @brief Get shape for tensor.
* @ingroup ov_tensor_c_api
diff --git a/src/bindings/c/src/ov_tensor.cpp b/src/bindings/c/src/ov_tensor.cpp
index 30674e3df78..eda445e41ce 100644
--- a/src/bindings/c/src/ov_tensor.cpp
+++ b/src/bindings/c/src/ov_tensor.cpp
@@ -83,6 +83,28 @@ ov_status_e ov_tensor_create_from_host_ptr(const ov_element_type_e type,
return ov_status_e::OK;
}
+ov_status_e ov_tensor_create_from_string_array(const char** string_array,
+ const size_t array_size,
+ const ov_shape_t shape,
+ ov_tensor_t** tensor) {
+ if (!tensor || !string_array) {
+ return ov_status_e::INVALID_C_PARAM;
+ }
+ try {
+ auto _tensor = std::make_unique<ov_tensor_t>();
+ ov::Shape tmp_shape;
+ std::copy_n(shape.dims, shape.rank, std::back_inserter(tmp_shape));
+ if (shape_size(tmp_shape) < array_size) {
+ return ov_status_e::INVALID_C_PARAM;
+ }
+ _tensor->object = std::make_shared<ov::Tensor>(ov::element::string, tmp_shape);
+ std::copy_n(string_array, array_size, _tensor->object->data<std::string>());
+ *tensor = _tensor.release();
+ }
+ CATCH_OV_EXCEPTIONS
+ return ov_status_e::OK;
+}
+
ov_status_e ov_tensor_set_shape(ov_tensor_t* tensor, const ov_shape_t shape) {
if (!tensor) {
return ov_status_e::INVALID_C_PARAM;
diff --git a/src/bindings/c/tests/ov_tensor_test.cpp b/src/bindings/c/tests/ov_tensor_test.cpp
index e1f7399ae8e..ceb9129b6f5 100644
--- a/src/bindings/c/tests/ov_tensor_test.cpp
+++ b/src/bindings/c/tests/ov_tensor_test.cpp
@@ -31,6 +31,17 @@ TEST(ov_tensor, ov_tensor_create_from_host_ptr) {
ov_shape_free(&shape);
}
+TEST(ov_tensor, ov_tensor_create_from_string_array) {
+ const char* string_array[4] = {"test", "me", "hi", "there"};
+ ov_tensor_t* tensor = nullptr;
+ ov_shape_t shape;
+ const int64_t dims[2] = {2, 2};
+ ov_shape_create(2, dims, &shape);
+ OV_EXPECT_OK(ov_tensor_create_from_string_array(string_array, 4, shape, &tensor));
+ EXPECT_NE(nullptr, tensor);
+ ov_tensor_free(tensor);
+}
+
static size_t product(const std::vector<size_t>& dims) {
if (dims.empty())
return 0;