TensorFlow
C++ API
ClientSession
Session
Machine Learning

Difference between ClientSession and Session in TensorFlow C API

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

ClientSession and Session solve related but different problems in TensorFlow’s native APIs. The short version is that ClientSession is a higher-level C++ convenience wrapper built to work with the Scope and op-wrapper API, while Session is the lower-level execution interface that runs graph operations directly. The title often says “C API,” but that is already a clue that something is mixed up: ClientSession belongs to the C++ API, not the plain C API.

Start With the Important Naming Correction

TensorFlow exposes both a C API and a C++ API.

  • the C API revolves around types such as TF_Graph and TF_Session
  • the C++ API includes classes such as Scope, generated op wrappers, and ClientSession

So if you are comparing ClientSession and Session, you are really in TensorFlow C++, even if the question mentions “C API.” That distinction matters because the abstractions and ergonomics are very different.

What Session Represents

Session is the fundamental engine that executes a graph. It gives you direct control over graph loading, feeds, fetches, targets, and session options.

A typical lower-level pattern looks like this conceptually:

cpp
1#include "tensorflow/core/public/session.h"
2#include "tensorflow/core/platform/env.h"
3
4using namespace tensorflow;
5
6int main() {
7    SessionOptions options;
8    std::unique_ptr<Session> session(NewSession(options));
9
10    GraphDef graph_def;
11    // graph_def would normally be loaded or built here
12    TF_CHECK_OK(session->Create(graph_def));
13
14    std::vector<Tensor> outputs;
15    TF_CHECK_OK(session->Run({}, {"output_node:0"}, {}, &outputs));
16
17    TF_CHECK_OK(session->Close());
18    return 0;
19}

This level is flexible, but you are responsible for more setup. It is the right fit when you are loading a saved graph, integrating with infrastructure, or needing lower-level control.

What ClientSession Adds

ClientSession is a convenience class for the higher-level C++ graph-construction API. Instead of building raw node definitions manually, you create ops through Scope and run them through a session wrapper that understands those objects naturally.

cpp
1#include "tensorflow/cc/client/client_session.h"
2#include "tensorflow/cc/ops/standard_ops.h"
3
4using namespace tensorflow;
5using namespace tensorflow::ops;
6
7int main() {
8    Scope root = Scope::NewRootScope();
9
10    auto a = Const(root, 3.0f);
11    auto b = Const(root, 4.0f);
12    auto c = Add(root, a, b);
13
14    ClientSession session(root);
15    std::vector<Tensor> outputs;
16    TF_CHECK_OK(session.Run({c}, &outputs));
17
18    std::cout << outputs[0].scalar<float>()() << std::endl;
19    return 0;
20}

This is shorter and easier to read because ClientSession handles a lot of the graph plumbing for you.

Practical Difference in Abstraction Level

The real difference is not “one runs graphs and one does not.” Both are about execution. The difference is how much graph construction and session wiring they expose.

Use ClientSession when:

  • you are building graphs with Scope and C++ op wrappers
  • you want concise code for experiments or smaller native integrations
  • you prefer TensorFlow’s object-style C++ API

Use Session when:

  • you need direct control over graph creation and execution
  • you are loading GraphDef objects explicitly
  • you are working closer to the underlying runtime concepts

ClientSession is essentially a friendlier face on top of session execution for the C++ op-wrapper workflow.

How This Relates to the True C API

The true C API does not provide ClientSession. It provides lower-level constructs such as TF_Session, TF_Graph, and TF_SessionRun.

That means if you are writing plain C, or wrapping TensorFlow for another language through the C API, the relevant mental model is much closer to Session than to ClientSession.

So a good rule is:

  • if you see Scope, ops::Add, or ClientSession, you are in TensorFlow C++
  • if you see TF_Graph and TF_SessionRun, you are in the C API

Choose Based on the Code You Already Have

Do not migrate between the two just because one sounds more “official.” Pick the layer that matches the rest of your integration.

If your code already uses generated C++ op wrappers, ClientSession is usually the natural choice. If your code works with serialized graphs, custom runtime setup, or C-style bindings, using Session or the true C API is more coherent.

The wrong choice is mixing the abstractions casually and then fighting type conversions and graph ownership.

Common Pitfalls

A common mistake is thinking ClientSession is part of the C API. It is not. It belongs to the higher-level C++ API.

Another issue is using Session for simple op-wrapper examples and writing far more boilerplate than necessary. If you are already in Scope-based code, ClientSession is usually easier.

Developers also sometimes assume ClientSession and Session are interchangeable wrappers with identical responsibilities. They are related, but they live at different abstraction levels and are meant for different workflows.

Finally, avoid mixing raw C API concepts with C++ convenience APIs unless you have a strong reason and understand the ownership boundaries.

Summary

  • 'ClientSession is a higher-level C++ helper for running graphs built with Scope and op wrappers.'
  • 'Session is the lower-level execution interface for graph running and explicit control.'
  • The plain C API does not include ClientSession.
  • If the code is built around C++ op wrappers, ClientSession is usually the cleaner choice.
  • If you need lower-level graph management or are using the real C API, think in terms of Session-style execution instead.

Course illustration
Course illustration

All Rights Reserved.