jackwener/OpenCLI · error

${label} returned malformed items payload

Error message

${label} returned malformed items payload

What it means

getPostFromFeed requires the parsed payload to be an object whose .items is an array; otherwise it throws '${label} returned malformed items payload'. It guards against Instagram schema changes or error payloads that parse as JSON but lack the expected feed structure.

Source

Thrown at clis/instagram/comment.js:37

    pipeline: [
        { navigate: 'https://www.instagram.com' },
        { evaluate: `(async () => {
  const username = \${{ args.username | json }};
  const commentText = \${{ args.text | json }};
  const idx = \${{ args.index }} - 1;
  if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');
  const headers = { 'X-IG-App-ID': '936619743392459' };
  const opts = { credentials: 'include', headers };
  async function readInstagramJson(response, label) {
    try {
      return await response.json();
    } catch {
      throw new Error(label + ' returned invalid JSON');
    }
  }
  function getPostFromFeed(feed, label) {
    if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {
      throw new Error(label + ' returned malformed items payload');
    }
    if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');
    const post = feed.items[idx];
    const pkRaw = post?.pk ?? post?.id;
    const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');
    if (!/^\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');
    return { pk };
  }
  function assertOkStatus(payload, label) {
    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {
      throw new Error(label + ' returned no success evidence');
    }
  }

  // web_profile_info answers HTTP 400 for business accounts; feed-by-username needs no user id. See #2234.
  const r1 = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=' + (idx + 1), opts);
  if (!r1.ok) throw new Error(r1.status === 404 ? 'User not found: ' + username : 'HTTP ' + r1.status + ' - make sure you are logged in to Instagram');
  const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Inspect the actual JSON payload (log it) to see the new/alternate shape and adapt parsing
  2. Re-authenticate with fresh cookies — flagged sessions often get stub payloads
  3. Verify the target account is public and exists (private/deleted accounts can yield non-feed payloads)
  4. Update the CLI to support the new Instagram response schema if it changed

Example fix

// before
function getPostFromFeed(feed, label) {
  if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {
    throw new Error(label + ' returned malformed items payload');
  }
// after
function getPostFromFeed(feed, label) {
  const items = feed?.items ?? feed?.data?.items ?? feed?.edge_owner_to_timeline_media?.edges;
  if (!Array.isArray(items)) {
    throw new Error(label + ' returned malformed items payload: ' + JSON.stringify(feed).slice(0, 200));
  }
Defensive patterns

Strategy: type-guard

Validate before calling

const feed = await readInstagramJson(r1, 'feed');
if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {
  console.error('Unexpected payload:', JSON.stringify(feed).slice(0, 300));
}

Type guard

function isFeedPayload(f) {
  return !!f && typeof f === 'object' && Array.isArray(f.items);
}

Try / catch

try {
  await runCommentPipeline(args);
} catch (e) {
  if (/malformed items payload/.test(e.message)) {
    console.error('Instagram response schema changed or session flagged - log payload and adapt');
  } else throw e;
}

Prevention

When it happens

Trigger: feed-by-username endpoint returns 200 JSON that is not the expected shape — e.g. {status:'fail', message:...}, a GraphQL-shaped object without items, or an empty object from a soft-failed request.

Common situations: Instagram rolling out API schema changes; private/suspended accounts returning a non-feed payload; API responses that shift shape for flagged sessions.

Understand the failure class

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/26d367c795d7ef4a. Report an issue: GitHub.