{"record":{"id":"26d367c795d7ef4a","repo":"jackwener/OpenCLI","slug":"label-returned-malformed-items-payload","errorCode":null,"errorMessage":"${label} returned malformed items payload","messagePattern":"(.+?) returned malformed items payload","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/comment.js","lineNumber":37,"sourceCode":"    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const username = \\${{ args.username | json }};\n  const commentText = \\${{ args.text | json }};\n  const idx = \\${{ args.index }} - 1;\n  if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');\n  const headers = { 'X-IG-App-ID': '936619743392459' };\n  const opts = { credentials: 'include', headers };\n  async function readInstagramJson(response, label) {\n    try {\n      return await response.json();\n    } catch {\n      throw new Error(label + ' returned invalid JSON');\n    }\n  }\n  function getPostFromFeed(feed, label) {\n    if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {\n      throw new Error(label + ' returned malformed items payload');\n    }\n    if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');\n    const post = feed.items[idx];\n    const pkRaw = post?.pk ?? post?.id;\n    const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');\n    if (!/^\\\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');\n    return { pk };\n  }\n  function assertOkStatus(payload, label) {\n    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {\n      throw new Error(label + ' returned no success evidence');\n    }\n  }\n\n  // web_profile_info answers HTTP 400 for business accounts; feed-by-username needs no user id. See #2234.\n  const r1 = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=' + (idx + 1), opts);\n  if (!r1.ok) throw new Error(r1.status === 404 ? 'User not found: ' + username : 'HTTP ' + r1.status + ' - make sure you are logged in to Instagram');\n  const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/comment.js#L19-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"Instagram rolling out API schema changes; private/suspended accounts returning a non-feed payload; API responses that shift shape for flagged sessions.","solutions":["Inspect the actual JSON payload (log it) to see the new/alternate shape and adapt parsing","Re-authenticate with fresh cookies — flagged sessions often get stub payloads","Verify the target account is public and exists (private/deleted accounts can yield non-feed payloads)","Update the CLI to support the new Instagram response schema if it changed"],"exampleFix":"// before\nfunction getPostFromFeed(feed, label) {\n  if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {\n    throw new Error(label + ' returned malformed items payload');\n  }\n// after\nfunction getPostFromFeed(feed, label) {\n  const items = feed?.items ?? feed?.data?.items ?? feed?.edge_owner_to_timeline_media?.edges;\n  if (!Array.isArray(items)) {\n    throw new Error(label + ' returned malformed items payload: ' + JSON.stringify(feed).slice(0, 200));\n  }","handlingStrategy":"type-guard","validationCode":"const feed = await readInstagramJson(r1, 'feed');\nif (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {\n  console.error('Unexpected payload:', JSON.stringify(feed).slice(0, 300));\n}","typeGuard":"function isFeedPayload(f) {\n  return !!f && typeof f === 'object' && Array.isArray(f.items);\n}","tryCatchPattern":"try {\n  await runCommentPipeline(args);\n} catch (e) {\n  if (/malformed items payload/.test(e.message)) {\n    console.error('Instagram response schema changed or session flagged - log payload and adapt');\n  } else throw e;\n}","preventionTips":["Log full payloads when shape assertions fail","Keep the CLI updated for Instagram schema changes","Verify target account is public and active","Use fresh cookies - flagged sessions get stub payloads"],"tags":["instagram","schema-mismatch","api-response","validation"],"backgroundTag":"unexpected-api-response-schema","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}