Inferensys

Prompt

Conditional JSON Lines or JSON Array Prompt Template

A practical prompt playbook for using Conditional JSON Lines or JSON Array Prompt Template in production AI workflows.
Operations team reviewing AI workflow automation on laptop, workflow builder visible, casual office setup.
PROMPT PLAYBOOK

When to Use This Prompt

Define the job, reader, and constraints for the Conditional JSON Lines or JSON Array prompt.

This prompt is designed for data export pipelines and API endpoints that must dynamically choose between newline-delimited JSON (JSON Lines) for streaming ingestion and a standard JSON array for batch loading. The primary job-to-be-done is to accept a collection of records and a format selector, then produce a structurally valid output that matches the consumer's capability. The ideal user is a backend or data engineer building a multi-format export service who needs the model to enforce delimiter consistency for JSON Lines or bracket balancing for JSON arrays without manual post-processing.

Use this prompt when the output format must be conditional on a runtime flag such as format: "jsonl" or format: "array", and when the record count, consumer type, or downstream system requirements are not known at prompt design time. It is appropriate for systems that serve both real-time stream processors (which expect a \n delimiter between complete JSON objects) and batch ETL jobs (which expect a single valid JSON array). Do not use this prompt when the output is always a single record, when the format is statically known, or when you need streaming partial JSON chunks—those scenarios are better served by dedicated single-format or streaming-specific prompts.

Before deploying, you must pair this prompt with a validation harness that checks for the specific failure modes of each format: for JSON Lines, confirm that every line is a complete, parseable JSON object and that no extraneous text exists between delimiters; for JSON arrays, confirm bracket balancing, comma placement between elements, and that the entire payload parses as a single JSON document. If the output feeds a compliance or audit pipeline, add a human review step for the first export of a new data source. The next section provides the copy-ready prompt template you can adapt and wire into your application.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works, where it fails, and the operational prerequisites for production use.

01

Good Fit: Streaming Ingestion Pipelines

Use when: your consumer is a log aggregator, event hub, or stream processor that expects newline-delimited JSON (NDJSON) for incremental processing. Guardrail: set a record-count threshold (e.g., >100 records) to trigger JSON Lines output automatically.

02

Good Fit: Batch Export with Standard Consumers

Use when: downstream systems expect a single, parseable JSON array for bulk loading into a warehouse or object store. Guardrail: validate that the full array parses before returning a 200; on parse failure, fall back to JSON Lines with a warning header.

03

Bad Fit: Unbounded or Unknown Record Counts

Avoid when: the total record count is unknown at prompt time and the model must generate a complete JSON array. Large arrays risk truncation or malformed closing brackets. Guardrail: default to JSON Lines when record_count is absent or exceeds a safety limit.

04

Required Input: Format Selection Signal

Risk: without an explicit format flag or consumer_capability field, the model guesses and may produce a format the downstream system cannot ingest. Guardrail: require a format enum (json_array | json_lines) or a streaming boolean in every request payload.

05

Operational Risk: Delimiter Drift in JSON Lines

Risk: a single record containing an unescaped newline breaks the line-delimited contract, causing silent data loss or parser crashes. Guardrail: post-process every line with a JSON parser; reject or re-encode lines that fail. Log the line number and record ID on failure.

06

Operational Risk: Array Bracket Imbalance

Risk: model truncation or early stopping leaves a JSON array unclosed, breaking standard parsers. Guardrail: always validate the final character is ] for array output. If not, attempt bracket repair or fall back to JSON Lines with a partial-data warning.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt that selects between JSON Lines and a standard JSON array based on record count, consumer capability, or an explicit format flag.

This prompt template is designed for data export pipelines that must dynamically choose between newline-delimited JSON (JSON Lines) for streaming ingestion and a standard JSON array for batch loading. It accepts a set of records, a format selector, and optional constraints, then produces structurally valid output in the requested collection format. The template includes explicit instructions for delimiter consistency, bracket balancing, and edge-case handling when the record set is empty or contains a single item.

text
You are a data serialization engine. Your task is to output a collection of records in either JSON Lines (application/jsonlines) or a standard JSON array (application/json) based on the provided format selector.

## INPUT
[RECORDS]

## FORMAT SELECTOR
[FORMAT]
Valid values: "jsonl" | "json_array"

## CONSTRAINTS
[CONSTRAINTS]

## OUTPUT RULES

### If [FORMAT] is "jsonl":
- Output each record as a single, complete JSON object on its own line.
- Do not wrap the output in an array.
- Do not include a trailing newline after the last record unless [CONSTRAINTS] explicitly requires it.
- If [RECORDS] is empty, output nothing (zero bytes).
- Ensure every line is a parseable JSON object. No line should be empty or contain only whitespace.

### If [FORMAT] is "json_array":
- Output a single JSON array containing all records.
- The array must start with "[" and end with "]".
- If [RECORDS] is empty, output "[]".
- Each record must be a valid JSON object, separated by commas.
- Do not include trailing commas after the last element.

## GENERAL RULES
- Do not include any text outside the JSON output. No markdown fences, no explanations, no log lines.
- Preserve all fields, values, and ordering from [RECORDS] exactly as provided.
- If [CONSTRAINTS] includes a record limit, truncate the output to that limit and include a "truncated": true field in a metadata wrapper only if the wrapper is explicitly requested in [CONSTRAINTS].
- If [FORMAT] is not one of the valid values, default to "json_array" and include a warning field "format_warning": "invalid_format_defaulted_to_json_array" as the first element in the array.

To adapt this template for your pipeline, replace [RECORDS] with your serialized record set, [FORMAT] with the runtime selector value, and [CONSTRAINTS] with any additional rules such as record limits, field filtering, or metadata wrapping. Test the prompt with empty inputs, single-record inputs, and large record sets to verify delimiter consistency and bracket balancing. For high-throughput pipelines, pair this prompt with a post-generation validator that confirms each line parses as JSON (for JSON Lines) or that the full output parses as a single array (for JSON array mode) before the data enters downstream ingestion.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the conditional JSON Lines or JSON Array prompt. Each placeholder must be populated before the prompt is assembled and sent to the model.

PlaceholderPurposeExampleValidation Notes

[RECORDS]

The collection of data records to be serialized. Can be a list of objects, a list of lists, or a flat list of values.

[{"id": 1, "name": "alpha"}, {"id": 2, "name": "beta"}]

Must be a non-null iterable. Empty collections are allowed and should produce an empty array or empty file. Validate with isinstance check before prompt assembly.

[RECORD_COUNT]

The integer count of records in [RECORDS]. Used by the model to decide between JSON Lines and JSON Array without counting tokens.

2

Must be a non-negative integer. Must match the actual length of [RECORDS]. Mismatch causes format-selection errors. Validate with len() before injection.

[FORMAT_FLAG]

Explicit format override. Accepts 'jsonl', 'array', or 'auto'. When 'auto', the model uses [RECORD_COUNT] and [CONSUMER_CAPABILITY] to decide.

'auto'

Must be one of the three allowed enum values. Reject any other string. Case-insensitive normalization recommended before injection.

[CONSUMER_CAPABILITY]

Describes the downstream system's format support. Used when [FORMAT_FLAG] is 'auto' to select the appropriate collection format.

'streaming-ingestion-pipeline'

Must be a non-empty string. Common values: 'streaming-ingestion-pipeline', 'batch-loader', 'spreadsheet-import', 'human-review'. Unknown values should default to JSON Array for safety.

[OUTPUT_SCHEMA]

Optional JSON Schema describing the shape of each record. When provided, the model must validate that every output record conforms.

{"type": "object", "properties": {"id": {"type": "integer"}, "name": {"type": "string"}}, "required": ["id", "name"]}

If null, skip schema enforcement. If provided, must be a valid JSON Schema object. Validate with jsonschema.Draft7Validator before injection. Schema violations in output should trigger repair or retry.

[MAX_RECORDS_PER_CHUNK]

Upper bound on records per output chunk when streaming or paginating. Prevents memory exhaustion on large datasets.

1000

Must be a positive integer or null. When set, the model should split output into chunks of at most this size. Null means no chunking. Validate as int > 0 or None.

[INCLUDE_METADATA]

Boolean flag controlling whether a metadata wrapper with record count, format, and timestamp is prepended to the output.

Must be a strict boolean. When true, JSON Array output wraps records in an envelope with metadata. When true for JSON Lines, a single metadata line is prepended as the first line. Validate with isinstance(val, bool).

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the conditional JSON Lines or JSON Array prompt into a production data pipeline with validation, retries, and observability.

This prompt template is designed to sit inside a data export or API response pipeline where the output format must adapt to the consumer's ingestion capability. The harness should inject three critical runtime variables: the serialized record set, a record count, and a format selector flag. The format selector can be driven by an explicit consumer preference, a query parameter, or a threshold rule—for example, switching to JSON Lines when the record count exceeds 10,000 to enable streaming ingestion. The model's job is to apply the correct collection wrapper and delimiter discipline, not to transform individual record schemas. Keep record-level schema validation separate and run it before the records reach this prompt.

After the model responds, the harness must validate the structural envelope before releasing the payload. For json_array outputs, confirm the response parses as a single JSON array, that the opening and closing brackets are balanced, and that every element is a valid JSON object. For jsonl outputs, split on newline characters, reject any empty lines, and parse each non-empty line as a JSON object. A strict validator should also confirm that the line count matches the expected record count within an acceptable tolerance. If validation fails, implement a single retry with the same input but append a terse constraint reminder: 'The previous output failed structural validation. Ensure the output is valid [FORMAT] with correct delimiters and balanced brackets.' If the retry also fails, log the failure with the raw model output, the injected parameters, and the validator error, then fall back to returning a standard JSON array with an error envelope. For high-throughput pipelines, consider running the JSON Lines path through a streaming validator that checks each line as it arrives rather than buffering the entire response.

Model choice matters here. Use a model with strong JSON generation and instruction-following capabilities, such as GPT-4o, Claude 3.5 Sonnet, or an equivalent. Avoid smaller or older models that may drop closing brackets or inject commentary between JSON Lines records. Set temperature to 0 for deterministic format selection. Wire the prompt into your application with a thin adapter function that accepts a list of typed records, a format preference, and a consumer configuration object, then returns either a validated JSON array string or a validated JSON Lines string ready for streaming. Log every format decision, the record count, the validator result, and the final output byte size. This observability data will help you tune the record-count threshold and detect format drift before downstream consumers break.

IMPLEMENTATION TABLE

Expected Output Contract

Validation rules for the conditional JSON Lines or JSON Array output. Use this contract to build parser checks, schema validators, and integration tests before connecting the prompt to a data pipeline.

Field or ElementType or FormatRequiredValidation Rule

output_format

string enum: jsonl | json_array

Must match the resolved format decision. Reject if value is not one of the two allowed enum members.

records

array of objects (json_array) or newline-separated objects (jsonl)

If format is json_array, parse as a single JSON array. If jsonl, split on newline and parse each line as a JSON object. Reject if any line is not valid JSON.

records[].id

string or integer

Must be present in every record. Reject if any record is missing this field. No null or empty string allowed.

records[].data

object

Must be a valid JSON object per record. Reject if any record contains a non-object value or null for this field.

format_decision_reason

string

Must be a non-empty string explaining why the format was chosen. Required for audit trail and debugging.

record_count

integer

Must equal the actual number of records in the output. Reject on mismatch. Used for completeness verification.

truncated

boolean

Must be true if the output was truncated due to length limits, false otherwise. Reject if missing or not a boolean.

truncation_threshold

integer or null

If truncated is true, must be a positive integer indicating the max records allowed. If truncated is false, must be null.

PRACTICAL GUARDRAILS

Common Failure Modes

When a prompt must dynamically choose between JSON Lines and a JSON Array, the failure surface expands beyond schema validation into format selection logic, delimiter consistency, and consumer compatibility. These are the most common production failures and how to prevent them.

01

Format Flag Ignored or Misinterpreted

What to watch: The model ignores the [OUTPUT_FORMAT] flag and defaults to a JSON array regardless of the instruction. This often happens when the flag value is ambiguous (e.g., 'stream' vs 'jsonl') or when the prompt over-emphasizes array examples in few-shot demonstrations. Guardrail: Use an explicit, constrained enum for the format flag (json_array or json_lines). Include a pre-generation validation step that checks the flag value and injects a format-specific instruction token before the main prompt is assembled.

02

JSON Lines Delimiter Inconsistency

What to watch: The model produces a mix of valid newline-delimited records and malformed lines, such as missing newlines between records, trailing commas, or a dangling comma after the last record that breaks streaming parsers. Guardrail: Implement a post-generation validator that splits on newlines, attempts to parse each non-empty line as an independent JSON object, and rejects the entire payload if any line fails. For high-throughput pipelines, use a line-by-line repair loop that discards only the malformed lines and logs a warning.

03

Array Bracket Imbalance in Batch Mode

What to watch: When generating a JSON array, the model produces an unclosed bracket, a trailing comma before the closing bracket, or inserts a closing bracket prematurely, truncating the payload. This is common with long outputs or when the model hits a token limit mid-generation. Guardrail: Use a bracket-balancing post-processor that counts open and close brackets. If imbalanced, trigger a repair prompt that asks the model to complete the array. Set max_tokens high enough to accommodate the expected record count and include a stop sequence on the closing bracket to prevent runaway generation.

04

Record Count Threshold Flapping

What to watch: When the format selection is based on record count (e.g., 'use JSON Lines if > 100 records'), edge cases near the threshold cause inconsistent format selection across similar inputs, confusing downstream consumers. Guardrail: Implement hysteresis in the application layer, not the prompt. Use a stable threshold with a buffer zone (e.g., switch to JSON Lines at 100 records, but don't switch back to array until under 50). Log every format-selection decision with the record count and threshold for auditability.

05

Consumer Capability Mismatch

What to watch: The prompt selects a format based on a [CONSUMER_TYPE] flag, but the consumer's actual parsing capability differs from the declared type. For example, a consumer labeled 'stream_processor' may only accept JSON Lines with a specific envelope, not raw newline-delimited JSON. Guardrail: Maintain a consumer-capability registry in the application layer that maps consumer IDs to their exact format requirements, including envelope structure, delimiter style, and required metadata fields. Do not rely on the model to infer consumer capabilities from a label alone.

06

Mixed Format Output in a Single Response

What to watch: The model produces a JSON array wrapper but inserts JSON-Lines-style records inside the array elements, or vice versa. This hybrid output passes basic JSON validation but breaks downstream parsers that expect homogeneous record structures. Guardrail: Add a structural assertion after generation that checks the top-level type (array vs object-per-line) and rejects any response where the internal record structure doesn't match the selected format. For JSON Lines, confirm no outer array brackets exist. For JSON arrays, confirm the entire payload is a single valid array.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test whether the prompt reliably selects the correct collection format and produces structurally valid output. Each criterion targets a specific production failure mode for conditional JSON Lines vs JSON Array generation.

CriterionPass StandardFailure SignalTest Method

Format Selection Accuracy

Prompt selects JSON Lines when [RECORD_COUNT] exceeds threshold or [FORMAT_FLAG] is 'jsonl'; selects JSON Array otherwise

JSON Array produced for 10k+ records or JSON Lines produced for 3 records

Run 20 test cases varying [RECORD_COUNT] from 1 to 100,000 and [FORMAT_FLAG] across 'jsonl', 'json_array', and null; assert format matches selection rule

JSON Lines Delimiter Consistency

Every line is a complete, parseable JSON object terminated by a single newline; no blank lines; no trailing comma

Double newlines between records, trailing comma after last record, or records spanning multiple lines

Split output on newlines, filter empty strings, parse each non-empty line with JSON.parse; count parse failures

JSON Array Bracket Balancing

Output starts with '[' and ends with ']'; all objects separated by commas; no trailing comma before closing bracket

Unmatched brackets, missing opening or closing bracket, or trailing comma before ']'

Validate with JSON.parse on full output; also run bracket-counting check: open_bracket_count must equal close_bracket_count

Record Completeness Across Formats

Every record in either format contains all required fields from [OUTPUT_SCHEMA] with correct types; no fields dropped during format switch

Field present in JSON Array output but missing in JSON Lines output for same input data, or vice versa

Generate output in both formats from identical input; diff field sets per record; assert zero field-presence mismatches

Empty Result Set Handling

When [INPUT_DATA] contains zero records, output is '[]' for JSON Array mode or empty string for JSON Lines mode

Output contains 'null', the string 'empty', or a JSON object with an error message instead of the correct empty representation

Pass empty [INPUT_DATA] array; assert output is exactly '[]' or '' depending on selected format

Large Record Count Stability

Output for 50,000+ records completes without truncation, memory errors, or malformed final records

Output truncated mid-record, last line is partial JSON, or JSON Array missing closing bracket

Generate output for 100,000 synthetic records; validate final record is complete and parseable; check for closing bracket or valid final newline

Format Flag Precedence

When [FORMAT_FLAG] is explicitly set, it overrides any record-count-based selection logic

Record-count threshold overrides an explicit [FORMAT_FLAG] value, or flag is ignored entirely

Set [FORMAT_FLAG]='json_array' with [RECORD_COUNT]=1,000,000; assert JSON Array output; repeat with 'jsonl' and [RECORD_COUNT]=1

Consumer Capability Metadata

Output includes a 'format' field or wrapper indicating which format was selected and why, when [INCLUDE_METADATA] is true

No indication of selected format in output, or metadata claims 'json_array' when JSON Lines was produced

Assert metadata.format matches actual output format; assert metadata.selection_reason references [RECORD_COUNT] or [FORMAT_FLAG]

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base prompt and a simple [FORMAT_FLAG] variable set to jsonl or array. Use a lightweight script to count lines or validate JSON parsing. Skip schema enforcement initially—just check that the output parses and the record count matches expectations.

Prompt snippet

code
Output format: [FORMAT_FLAG]
If [FORMAT_FLAG] is 'jsonl', return one JSON object per line, no outer array.
If [FORMAT_FLAG] is 'array', return a single JSON array of objects.
Records: [RECORDS]

Watch for

  • Trailing commas in array mode breaking parsers
  • Blank lines in JSON Lines output causing parse failures
  • Model ignoring the flag and always producing arrays
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.