diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 438f9f23ee0..25f14f92370 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -5604,8 +5604,10 @@ parse_jsonb_index_flags(Jsonb *jb)
 	 * chosen the same as jsonb_typeof uses.
 	 */
 	if (type != WJB_BEGIN_ARRAY)
-		ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-						errmsg("wrong flag type, only arrays and scalars are allowed")));
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("incorrect flag structure"),
+				 errdetail("Flags argument must be a string or array of strings.")));
 
 	while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
 	{
@@ -5640,12 +5642,15 @@ parse_jsonb_index_flags(Jsonb *jb)
 
 	/* expect end of array now */
 	if (type != WJB_END_ARRAY)
-		elog(ERROR, "unexpected end of flag array");
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("incorrect flag structure"),
+				 errdetail("Flags argument must be a string or array of strings.")));
 
 	/* get final WJB_DONE and free iterator */
 	type = JsonbIteratorNext(&it, &v, false);
 	if (type != WJB_DONE)
-		elog(ERROR, "unexpected end of flag array");
+		elog(ERROR, "unexpected items after flag array");
 
 	return flags;
 }
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index 5411003e2e6..461c8781752 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -2785,7 +2785,8 @@ select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d":
 ERROR:  wrong flag in flag array: ""
 HINT:  Possible values are: "string", "numeric", "boolean", "key", and "all".
 select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '{}');
-ERROR:  wrong flag type, only arrays and scalars are allowed
+ERROR:  incorrect flag structure
+DETAIL:  Flags argument must be a string or array of strings.
 select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '[]');
  json_to_tsvector 
 ------------------
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index efcbcddc1b3..1e8bdd08168 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -5741,7 +5741,8 @@ select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d"
 ERROR:  wrong flag in flag array: ""
 HINT:  Possible values are: "string", "numeric", "boolean", "key", and "all".
 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '{}');
-ERROR:  wrong flag type, only arrays and scalars are allowed
+ERROR:  incorrect flag structure
+DETAIL:  Flags argument must be a string or array of strings.
 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '[]');
  jsonb_to_tsvector 
 -------------------
