Skip to content

Runtime SyntaxError for typescript 5.8.3 with openai/agents v0.0.12 at agents-core with node v22 locally and AWS lambda #245

@cam-nano

Description

@cam-nano

Please read this first

Yes

  • Have you searched for related issues? Others may have faced similar issues.

Closest one I could find was: #213

Describe the bug

Getting the issue when using the @openai/agent with a lambda (node22x) and locally (node v22.12.0). I have tried all sorts of permutations of tsconfig and removing dependencies but am still getting this error. I have a test repo for this issue that I can make public if needed.

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Cannot use import statement outside a module",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1106:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1148:21)",
    "    at async start (file:///var/runtime/index.mjs:1332:23)",
    "    at async file:///var/runtime/index.mjs:1339:1"
  ]
}

Getting a similar issue when running locally with node v22.12.0 as a unit test:

 /Users/cam/Documents/user/dev/test-openai/node_modules/@openai/agents/dist/index.js:1
    import { setDefaultModelProvider } from '@openai/agents-core';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import { Agent, run, setDefaultOpenAIKey } from '@openai/agents';
        | ^
      2 | import { OPENAI_API_KEY } from "../constants";
      3 | import { ClientError } from '../utils/client-error';
      4 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1314:40)
      at Object.<anonymous> (src/commands/process-agent-command.ts:1:1)
      at Object.<anonymous> (tests/agent-command.test.ts:1:1)

I have created a 'skinny' repo with only the handler, command and test in it and am getting the same result as in my larger app.

Debug information

  • Agents SDK version: (e.g. v0.0.1)
"@openai/agents": "^0.0.12",
  • Runtime environment (e.g. Node.js 22.16.0)

locally: v22.12.0
AWS lambda: node22x

Repro steps

tsconfig:

{
  "compilerOptions": {
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "isolatedModules": true,
    "outDir": "../lib",
    "removeComments": true,
    "sourceMap": true,
    "strictPropertyInitialization": false,
  },

  "extends": "@tsconfig/node22/tsconfig.json",
  "exclude": ["../tests/**/*.ts"]
}

Command:

import { Agent, run, setDefaultOpenAIKey } from '@openai/agents';
import { OPENAI_API_KEY } from "../constants";
import { ClientError } from '../utils/client-error';


export interface ProcessAgentCommandParams {
    userId: string;
    ledgerId?: string;
    question: string;
}

export interface ProcessAgentCommandResponse {
    answer?: string;
}

export class ProcessAgentCommand {
    params: ProcessAgentCommandParams;
    agent: Agent;
    
    private constructor(params: ProcessAgentCommandParams) {
        this.params = params;
        setDefaultOpenAIKey(OPENAI_API_KEY!);        
    }

    public static async build(params: ProcessAgentCommandParams): Promise<ProcessAgentCommand | ClientError> {
        const command = new ProcessAgentCommand(params)
        return command;
    }

    public async execute(): Promise<ProcessAgentCommandResponse> {
        const result = await run(
            this.agent,
            this.params.question,
        );
        console.log(result.finalOutput);

        return {
            answer: result.finalOutput,
        }
    }        
}

test


import { ProcessAgentCommand } from "../src/commands/process-agent-command";
import { ClientError } from "../src/utils/client-error";

describe('Agent Test', () => {
    it('should test the agent command', async () => {

        const command = await ProcessAgentCommand.build({
            userId: 'some-user-id',
            ledgerId: 'some-ledger-id',
            question: 'What is the capital of France?',
        });
        if (command instanceof ClientError) {
            console.error('Error building command:', command);
            return;
        }
        const result = await command.execute();
        console.log('Command executed successfully:', result);
    });
});

package.json dependencies, I have them in a skinny repo to try and isolate if a dependency is causing the issue:

{
   "dependencies": {
        "@openai/agents": "^0.0.12",
        "@types/aws-lambda": "^8.10.152",
        "uuid": "^11.1.0",
        "zod": "<=3.25.67"
    },
    "devDependencies": {
        "@aws-sdk/credential-providers": "3.848.0",
        "@aws-sdk/types": "3.840.0",
        "@tsconfig/node22": "22.0.2",
        "@types/jest": "^30.0.0",
        "@types/node": "^24.0.5",
        "aws-sdk-client-mock": "^4.1.0",
        "eslint": "9.31.0",
        "jest": "30.0.4",
        "prettier": "3.6.2",
        "ts-jest": "29.4.0",
        "typescript": "5.8.3",
        "typescript-eslint": "8.38.0"
    }
}

Expected behavior

I would expect there not to be "SyntaxError: Cannot use import statement outside a module" at " import { setDefaultModelProvider } from '@openai/agents-core';" when the runtime typescript is executed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions