Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 3 min read

Adding a humanize step to an LLM content pipeline in Python

Full disclosure: I'm the founder of RewriteAI, so I use its API in the examples below. The pattern works with any rewriting step, though. If you generate drafts with an LLM, you have probably noticed that the raw output

Full disclosure: I'm the founder of RewriteAI, so I use its API in the examples below. The pattern works with any rewriting step, though.

If you generate drafts with an LLM, you have probably noticed that the raw output has a recognizable rhythm. Same sentence length, same transitions, same tidy three-part lists. A lot of teams now add a separate rewrite step after generation, before a human editor looks at the text. This post shows a small, practical way to wire that up in Python.

The shape of the pipeline

  1. Generate a draft with your LLM of choice.
  2. Split the draft into chunks that fit your plan's per-request limit.
  3. Send each chunk to a rewriting API.
  4. Stitch the results back together and hand them to a human for review.

Step 4 matters more than people expect. A rewrite step changes style, not facts. Someone still has to check the claims, names and numbers.

The API call

The RewriteAI Humanizer API is one POST endpoint. You send text, you get back a list of results (one or more variants) and a wordsUsed count.

import os
import requests

API_URL = "https://rewriteai.com/api/v1/humanize"
API_KEY = os.environ["REWRITEAI_API_KEY"]


def humanize(text):
    resp = requests.post(
        API_URL,
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json",
        },
        json={"text": text},
        timeout=60,
    )
    resp.raise_for_status()
    data = resp.json()
    return data["results"][0]["text"], data["wordsUsed"]

I take the first variant here. If you want to pick the best of several, loop over data["results"] and score them with whatever check you trust, even something as simple as a length and keyword check against the original.

Chunking long drafts

Each request has a word limit that depends on your plan, so a long article has to be split. Splitting on paragraph boundaries keeps the meaning of each chunk intact, which gives better output than cutting at a fixed character count.

def chunk_paragraphs(text, max_words=700):
    chunks, current, count = [], [], 0
    for para in text.split("\n\n"):
        words = len(para.split())
        if current and count + words > max_words:
            chunks.append("\n\n".join(current))
            current, count = [], 0
        current.append(para)
        count += words
    if current:
        chunks.append("\n\n".join(current))
    return chunks


def humanize_article(text):
    out, used = [], 0
    for chunk in chunk_paragraphs(text):
        rewritten, words = humanize(chunk)
        out.append(rewritten)
        used += words
    return "\n\n".join(out), used

Keep max_words safely under your plan's per-input limit. A single paragraph longer than the limit will still fail, so split those on sentences if your LLM tends to write long blocks.

Things I would add before production

  • Retries with backoff around the request, since any network call can fail.
  • A cost guard that stops the run if wordsUsed for one article is far above the draft length.
  • A diff step that flags numbers, dates and proper nouns that changed between the draft and the rewrite. This catches the rare case where a figure gets altered.
  • A human review gate. Always.

A note on responsible use

Rewrite your own drafts and be open about AI use where your employer, publisher or school asks for it. A rewriting step is a style tool. It is not a substitute for checking your facts or for following the rules of the place you publish.

If you want to try the endpoint, the free plan is enough to test the flow, and the API page has the full request and response format: RewriteAI Humanizer API.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.