{"record":{"id":"d5232559c816b1d3","repo":"jackwener/OpenCLI","slug":"returned-malformed-items-payload-d52325","errorCode":null,"errorMessage":" returned malformed items payload","messagePattern":" returned malformed items payload","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/unsave.js","lineNumber":35,"sourceCode":"    columns: ['status', 'user', 'post'],\n    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const username = \\${{ args.username | 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    const caption = typeof post?.caption?.text === 'string' ? post.caption.text.substring(0, 60) : '';\n    return { pk, caption };\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');","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/unsave.js#L17-L53","documentation":"The Instagram feed response parsed as JSON but did not have the expected shape: feed.items was missing or not an array. getPostFromFeed validates the payload structure before indexing. This means Instagram changed the response envelope or returned an error/status object instead of media items.","triggerScenarios":"feed/user/{username}/username/ returns JSON without an items array — empty account, restricted/private profile, or Instagram API shape change.","commonSituations":"Querying a private account you don't follow; account with zero posts; Instagram A/B response format differences for business accounts.","solutions":["Confirm the target account is public and has posts.","Verify you follow the account if it is private.","Check Instagram API changes; update the CLI if the items envelope moved."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"const isFeed = (f) => f && typeof f === 'object' && Array.isArray(f.items);","typeGuard":"function isItemsPayload(d) {\n  return !!d && typeof d === 'object' && Array.isArray(d.items);\n}\nif (!isItemsPayload(feed)) throw new Error('unexpected feed shape');","tryCatchPattern":"try {\n  await cli.unsave(user, index);\n} catch (e) {\n  if (e.message.includes('malformed items payload')) {\n    console.error('Feed shape unexpected — is the account public and does it have posts?');\n  } else throw e;\n}","preventionTips":["Verify the target account is public and has posts","Re-check for Instagram API shape changes","Validate response envelopes before indexing"],"tags":["instagram","api-response","schema-mismatch"],"backgroundTag":"schema-validation-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}