diff --git a/tensorflow/core/util/BUILD b/tensorflow/core/util/BUILD
index 654601c915d..12f11c2e20a 100644
--- a/tensorflow/core/util/BUILD
+++ b/tensorflow/core/util/BUILD
@@ -481,6 +481,7 @@ cc_library(
     ],
     deps = [
         "//tensorflow/core/platform:status",
+        "@com_google_absl//absl/strings",
     ],
 )
 
diff --git a/tensorflow/core/util/managed_stack_trace.h b/tensorflow/core/util/managed_stack_trace.h
index 89e1f28c8e7..f30a6fbabda 100644
--- a/tensorflow/core/util/managed_stack_trace.h
+++ b/tensorflow/core/util/managed_stack_trace.h
@@ -18,6 +18,7 @@ limitations under the License.
 
 #include <string>
 
+#include "absl/strings/match.h"
 #include "tensorflow/core/platform/status.h"
 
 namespace tensorflow {
@@ -33,6 +34,16 @@ using ToStackFramesFunctor = std::vector<StackFrame>(int, const StackTraceMap&,
                                                      const StackTraceFilter&,
                                                      bool, int);
 
+// Returns whether the given frame is internal to TF.
+inline bool IsInternalFrameForFilename(absl::string_view file_name) {
+  // Use a simple heuristic for now.
+  // TODO(cheshire): Build a more sophisticated mechanism, rely on @tf.export.
+  return (absl::StrContains(file_name, "tensorflow/python") ||
+          absl::StrContains(file_name, "tensorflow\\python")) &&
+         !absl::StrContains(file_name, "keras") &&
+         !absl::StrContains(file_name, "test.py");
+}
+
 // Language agnostic stack trace class. It only saves an id, and language
 // clients are responsible for managing the actual stack trace objects.
 class ManagedStackTrace {
diff --git a/tensorflow/python/util/tf_stack.cc b/tensorflow/python/util/tf_stack.cc
index bc25159bd0c..54b83794876 100644
--- a/tensorflow/python/util/tf_stack.cc
+++ b/tensorflow/python/util/tf_stack.cc
@@ -226,15 +226,6 @@ class StackTraceWrapper : public AbstractStackTrace {
         });
   }
 
-  static bool IsInternalFrameForFilename(absl::string_view file_name) {
-    // Use a simple heuristic for now.
-    // TODO(cheshire): Build a more sophisticated mechanism, rely on @tf.export.
-    return (absl::StrContains(file_name, "tensorflow/python") ||
-            absl::StrContains(file_name, "tensorflow\\python")) &&
-           !absl::StrContains(file_name, "keras") &&
-           !absl::StrContains(file_name, "test.py");
-  }
-
   absl::optional<StackFrame> StackTraceMapping(SourceLoc loc) const {
     if (source_map_->contains(loc)) {
       return source_map_->at(loc);