Importance of context in a chatbot conversation

Share this post:

This post introduces the Importance of Context to maintain the state of a Conversation while building a bot more specifically a chatbot.

A conversation (or chat) is a chain of statements exchanged between two or more individuals. Mostly, conversations happen on a particular topic or in a situation. Whatever the topic or situation is, Context is very important to maintain the state of a conversation.

Conversation Chatbot

In the world of cognitive computing, computers are conversing with humans as ChatBots or in short Bots. As Alan Turing famously quoted

A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.

So, even computers need the so called Context to keep the conversation flowing and to deceive us as humans.

Why am I so contextual today? Looks like chatting with a Bot for a week took a toll on me or reading the below line from the Watson Conversation documentation had its impact,

State information for your conversation is maintained using the context. The context is a JSON object that is passed back and forth between your application and the Conversation service. It is the responsibility of your application to maintain the context from one turn of the conversation to the next.


A context includes a unique identifier for each conversation with a user, as well as a counter that is incremented with each turn of the conversation. If we don’t preserve the context,  each round of input appeared to be the start of a new conversation. We can fix that by saving the context and sending it back to the Conversation service each time.

Code to achieve Context persistence

In Javascript,

// maintains state.

var prompt = require('prompt-sync')();
var ConversationV1 = require('watson-developer-cloud/conversation/v1');

// Set up Conversation service.
var conversation = new ConversationV1({
username: 'USERNAME', // replace with username from service key
password: 'PASSWORD', // replace with password from service key
path: { workspace_id: 'WORKSPACE_ID' }, // replace with workspace ID
version_date: '2016-07-11'

// Start conversation with empty message.
conversation.message({}, processResponse);

// Process the conversation response.
function processResponse(err, response) {
if (err) {
console.error(err); // something went wrong

// If an intent was detected, log it out to the console.
if (response.intents.length > 0) {
console.log('Detected intent: #' + response.intents[0].intent);

// Display the output from dialog, if any.
if (response.output.text.length != 0) {

// Prompt for the next round of input.
var newMessageFromUser = prompt('>> ');
// Send back the context to maintain state.
input: { text: newMessageFromUser },
context : response.context,
}, processResponse)

In Android, using Watson Developer Cloud Java SDK

private Map<String,Object> context = new HashMap<>();

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
service.setUsernameAndPassword("Your Watson service UserName", "Your watson service PassWord");
MessageRequest newMessage = new MessageRequest.Builder().inputText(inputmessage).context(context).build();
MessageResponse response = service.message("Your Workspace Id", newMessage).execute();

//Passing Context of last conversation
if(response.getContext() !=null)
context = response.getContext();


Check the complete Android code –  ChatBot Repo on Github

In Swift 3.0,  using swift watson sdk

import Conversation

let username = "your-username-here"
let password = "your-password-here"
let version = "YYYY-MM-DD" // use today's date for the most recent version
let conversation = Conversation(username: username, password: password, version: version)

let workspaceID = "your-workspace-id-here"
let failure = { (error: RestError) in print(error) }
var context: Context? // save context to continue conversation
conversation.message(workspaceID, failure: failure) { response in
context = response.context

In addition to maintaining our place in the conversation, the context can also be used to store any other data you want to pass back and forth between your application and the Conversation service. This can include persistent data you want to maintain throughout the conversation (such as a customer’s name or account number), or any other data you want to track (such as the current status of option settings).


  • Check Code Samples leveraging the power of Watson conversation Service on IBM Cloud.
  • Watson Developer Cloud SDKs 

Technical Offering Manager & Polyglot Programmer | IBM Cloud

More How-tos stories
April 23, 2019

Announcing the Deprecation of the Watson Machine Learning JSON Token Authentication Service

We’d like to inform you about the deprecation of the Watson Machine Learning JSON Token Authentication service. This method of authentication will be retired on May 30, 2019.

Continue reading

April 2, 2019

Data Refinery and Profiling Changes in Watson Studio and Watson Knowledge Catalog

We'd like to announce data refinery and profiling changes related to Watson Studio and Watson Knowledge Catalog that will take effect on May 17, 2019.

Continue reading

March 26, 2019

Announcement for the IBM Watson Discovery Community

Starting April 2, 2019, we will be upgrading the infrastructure of the IBM Watson Discovery service to improve reliability and quality of results.

Continue reading