G-XR8P2XJ088

AI-Written CSV Exports: Decide Which Columns Leave the Shop

Imagine an independent print shop sending a daily order file to a fulfilment partner. An AI assistant writes the exporter. Everything works until the order form gains a field for collection times. The next export either stops on the unfamiliar field or produces a familiar-looking file that leaves the new instruction out. Both outcomes make sense to the software. Only one might make sense to the person packing the order, and that depends on whether the partner is supposed to act on the new information.

Before asking an assistant to make the error go away, decide what the recipient needs and what should stay in the shop. A production note can be useful internally and irrelevant to a partner. A promised collection time may be essential. The fact that both are new keys in a Python dictionary does not make them the same kind of change.

The column list is a decision about the handover

Python’s documentation for csv.DictWriter describes two separate decisions. fieldnames gives the keys to write and determines their column order. If an input row contains a key outside that list, the default extrasaction="raise" rejects the row with a ValueError. Setting extrasaction="ignore" leaves out those extra keys. Neither choice tells Python which of your columns the partner considers mandatory.

Here is a small example of a deliberately narrow export. The incoming record contains an internal note, but the writer is given a new dictionary containing only the two agreed fields. The example leaves the default error policy explicit so an unplanned key in that projected row is visible during testing.

import csv
from io import StringIO

columns = ["order_id", "quantity"]
record = {"order_id": "P-204", "quantity": 3,
          "internal_note": "Check paper stock"}

output = StringIO(newline="")
writer = csv.DictWriter(output, fieldnames=columns,
                        extrasaction="raise")
writer.writeheader()
writer.writerow({name: record[name] for name in columns})

The example writes order_id,quantity as the header and P-204,3 as the data row. Here the projection is where the choice happens: it selects the fields that cross the boundary. That means the writer cannot warn about a new key in the original record that never reaches it. If upstream changes need review, compare the incoming keys against the fields you expect there, separately from how you format the outgoing file. The two lists serve different purposes.

A missing field needs its own rule

With a dictionary passed directly to the writer, a missing listed key is filled using restval, which defaults to an empty string. It does not trigger the extra-key error. In the example above, a missing required key instead fails when the projection looks it up. For a real batch, check required keys first and report the affected record with a message a colleague can act on. An empty value can still be present, so a required collection time needs a separate check if a blank time is unusable.

Optional fields can follow a different rule. A gift message might legitimately be blank. An unknown collection time may need to hold the order for review. Put each rule beside the relevant field in the brief you give the assistant. If the existing feed uses a blank for several meanings, ask the recipient how it interprets that blank before adding a new convention. A tidy file is little help if the warehouse reads an empty cell differently from the shop.

Think about old orders when a new field becomes mandatory. Orders placed before the change may have no collection time because the shop never asked for one. Adding the heading to every export will not supply that missing information. In a small trial, send the partner a sample covering an old order, a new order and one that cannot yet be fulfilled. Agree whether the old orders need manual review or can still use the previous handover. Keep a copy of the agreed headings and sample alongside the export instructions, so an editor can see when a change requires the recipient’s involvement.

Test the file from the recipient’s side

A rejected row can arrive after the exporter has written earlier rows. If the receiving process can see the file while it is being assembled, use a separate working location and release the result only after the batch passes its checks. Keep the last successful batch identifiable as well. The recipient should be able to tell whether it is looking at a fresh file or yesterday’s copy.

  • Add a field that should stay inside the shop. Confirm it never appears in the headings or data, even when the input contains it.
  • Add a field the partner now needs. Check that the file and the receiver agree on the new column before relying on it. A projected export can otherwise drop it without complaint.
  • Try missing and blank values separately. A record without a required key and a record with an empty value should each have an explicit outcome.
  • Open the finished file in the receiving workflow. Check the header order, a sample of values and the expected number of rows. Include a failing record late in a test batch and confirm that a partial file is kept out of the handover.

MaryChuks.com’s guide to duplicate CSV headings on import covers the other side of this exchange. Check both descriptions of the file: what your exporter promises to write and what the partner’s importer actually uses. If they disagree, settle that before changing the script. The point of the column list is to make the handover predictable for the people doing the work.

Source


Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe to get the latest posts sent to your email.

2 thoughts on “AI-Written CSV Exports: Decide Which Columns Leave the Shop”

Leave a Reply

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe now to keep reading and get access to the full archive.

Continue reading