How to intermediate · 3 min read

Handle partial workflow completion

Quick answer
Use checkpointing by saving intermediate AI responses and workflow states to resume later. With OpenAI SDK, store partial outputs and pass them back as context to continue the workflow seamlessly.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable.

  • Install package: pip install openai
  • Set environment variable: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example demonstrates saving partial workflow results and resuming by passing saved context back to the OpenAI chat model.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Simulated persistent storage for partial workflow state
workflow_state = {}

# Step 1: Start workflow and save partial output
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Generate a summary of AI workflows."}
]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
partial_output = response.choices[0].message.content

# Save partial output to state
workflow_state["summary"] = partial_output
print("Partial output saved:", partial_output)

# Step 2: Resume workflow using saved partial output
resume_messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": f"Continue from this summary: {workflow_state['summary']} Add examples."}
]
resume_response = client.chat.completions.create(model="gpt-4o-mini", messages=resume_messages)
final_output = resume_response.choices[0].message.content
print("Final output after resuming workflow:", final_output)
output
Partial output saved: AI workflows involve orchestrating multiple AI tasks such as data preprocessing, model inference, and postprocessing.
Final output after resuming workflow: For example, you can start with data cleaning, then run a GPT model for text generation, and finally summarize results for reporting.

Common variations

You can implement partial workflow completion with different approaches:

  • Use async calls with asyncio to handle long-running steps.
  • Stream partial outputs using stream=True in chat.completions.create for real-time updates.
  • Use different models like gpt-4o-mini for cost-effective partial completions.
  • Store partial states in databases or files for persistent checkpointing.
python
import asyncio
import os
from openai import OpenAI

async def async_partial_workflow():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Generate a list of AI workflow steps."}
    ]

    # Async streaming partial output
    stream = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        stream=True
    )

    partial_text = ""
    async for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        partial_text += delta
        print(delta, end="", flush=True)

    print("\nPartial workflow output complete.")

asyncio.run(async_partial_workflow())
output
Step 1: Data collection
Step 2: Data preprocessing
Step 3: Model training
Step 4: Model evaluation
Step 5: Deployment
Partial workflow output complete.

Troubleshooting

  • If partial outputs are missing or incomplete: Ensure you save the entire response.choices[0].message.content before continuing.
  • If context length exceeds model limits: Trim or summarize saved partial states before resuming.
  • API errors on resume calls: Verify your API key and model name are correct and environment variables are set.
  • Streaming hangs or incomplete data: Confirm network stability and use proper async iteration over the stream.

Key Takeaways

  • Save intermediate AI responses as workflow checkpoints to enable resuming later.
  • Pass saved partial outputs back as context in subsequent API calls to continue workflows.
  • Use streaming and async calls for real-time partial completion handling.
  • Manage context length carefully to avoid exceeding model token limits.
  • Persist workflow state externally for robust partial completion across sessions.
Verified 2026-04 · gpt-4o-mini
Verify ↗