Skip to content

Debug: File parts test script for withTracing error reproduction#29

Draft
andrewm4894 wants to merge 2 commits intomainfrom
andy/file-parts-debug
Draft

Debug: File parts test script for withTracing error reproduction#29
andrewm4894 wants to merge 2 commits intomainfrom
andy/file-parts-debug

Conversation

@andrewm4894
Copy link
Copy Markdown
Member

@andrewm4894 andrewm4894 commented Jan 7, 2026

Test Scripts for File Parts Error Reproduction

Test scripts to help reproduce and debug the "Cannot convert undefined or null to object" error reported when using withTracing with file parts in Vercel AI SDK v6.

Scripts

scripts/test-file-parts.ts

Tests various file part configurations with generateText:

  • base64 string data
  • data URL string
  • Uint8Array
  • Buffer
  • ArrayBuffer
  • null/undefined data (edge cases)
  • Streaming variants
npx ts-node --esm scripts/test-file-parts.ts --gemini

scripts/test-agent-file-parts.ts

Tests ToolLoopAgent with file parts:

  • agent.generate() with file attachments
  • agent.stream() with file attachments
npx ts-node --esm scripts/test-agent-file-parts.ts
npx ts-node --esm scripts/test-agent-file-parts.ts --stream

Results

Tests pass with current SDK versions (PostHog AI v6.6.0, Vercel AI SDK v6), suggesting the issue is environment-specific.

Related

Test script to help reproduce "Cannot convert undefined or null to object"
error reported with withTracing and file parts in Vercel AI SDK v6.

Tests various file part configurations:
- base64 string data
- data URL string
- Uint8Array
- Buffer
- ArrayBuffer
- null/undefined data (edge cases)
- Streaming variants

Run with: npx ts-node --esm scripts/test-file-parts.ts --gemini

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

// Enable debug mode to see PostHog events
posthog.debug(true);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High test

This logs sensitive data returned by
an access to POSTHOG_API_KEY
as clear text.

Copilot Autofix

AI 4 months ago

To fix this, we should stop logging the actual value (even partially) of POSTHOG_API_KEY and instead log only non-sensitive configuration aspects or boolean-style information (e.g., whether the key is set). This preserves the existing functionality of debugging PostHog configuration without exposing secrets.

The best, minimal-change fix is to modify line 38 to remove ${process.env.POSTHOG_API_KEY?.substring(0, 10)}... and replace it with something that does not reveal the key contents, such as a boolean or masked placeholder. For example: key=${process.env.POSTHOG_API_KEY ? '[set]' : '[missing]'}. This requires no new imports or additional helper functions and keeps the console output informative.

Concretely, in node/scripts/test-file-parts.ts, replace the console.log call on line 38 with one that does not interpolate the key value, only whether it exists. No other lines or files need to change.

Suggested changeset 1
node/scripts/test-file-parts.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/node/scripts/test-file-parts.ts b/node/scripts/test-file-parts.ts
--- a/node/scripts/test-file-parts.ts
+++ b/node/scripts/test-file-parts.ts
@@ -35,7 +35,7 @@
 
 // Enable debug mode to see PostHog events
 posthog.debug(true);
-console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);
+console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY ? '[set]' : '[missing]'}`);
 
 const openaiClient = createOpenAI({
   apiKey: process.env.OPENAI_API_KEY!
EOF
@@ -35,7 +35,7 @@

// Enable debug mode to see PostHog events
posthog.debug(true);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY ? '[set]' : '[missing]'}`);

const openaiClient = createOpenAI({
apiKey: process.env.OPENAI_API_KEY!
Copilot is powered by AI and may make mistakes. Always verify output.
Test script to reproduce "Cannot convert undefined or null to object"
error with ToolLoopAgent and file attachments.

Tests both generate() and stream() methods with file parts using
the Gemini provider.

Related: PostHog/posthog-js#2877

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
);

posthog.debug(true);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High test

This logs sensitive data returned by
an access to POSTHOG_API_KEY
as clear text.

Copilot Autofix

AI 4 months ago

In general, the fix is to avoid logging secrets such as API keys entirely, or to log only non-sensitive metadata (e.g., whether a key is present, obfuscated length, or host information). Any diagnostic logging should be careful not to include raw or partially raw secrets.

For this specific case, the best fix without changing functionality is to stop including process.env.POSTHOG_API_KEY in the log line. The rest of the script only needs to know that PostHog is configured; it doesn’t require the key value to be printed. We can keep logging the PostHog host and optionally a boolean flag indicating whether a key is present (which is not sensitive). Concretely, in node/scripts/test-agent-file-parts.ts, replace line 37 so that it either removes the key altogether or replaces it with a non-sensitive indicator like key_present=${Boolean(process.env.POSTHOG_API_KEY)}.

No new helper methods or complex logic are needed; just update the log statement. No new imports are required.

Suggested changeset 1
node/scripts/test-agent-file-parts.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/node/scripts/test-agent-file-parts.ts b/node/scripts/test-agent-file-parts.ts
--- a/node/scripts/test-agent-file-parts.ts
+++ b/node/scripts/test-agent-file-parts.ts
@@ -34,7 +34,7 @@
 );
 
 posthog.debug(true);
-console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);
+console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key_present=${Boolean(process.env.POSTHOG_API_KEY)}`);
 
 const googleClient = createGoogleGenerativeAI({
   apiKey: process.env.GEMINI_API_KEY!
EOF
@@ -34,7 +34,7 @@
);

posthog.debug(true);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key=${process.env.POSTHOG_API_KEY?.substring(0, 10)}...`);
console.log(`\n📊 PostHog Config: host=${process.env.POSTHOG_HOST}, key_present=${Boolean(process.env.POSTHOG_API_KEY)}`);

const googleClient = createGoogleGenerativeAI({
apiKey: process.env.GEMINI_API_KEY!
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants