Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracing in JS is broken #1274

Closed
kyzyx opened this issue Dec 29, 2024 · 0 comments · Fixed by #1275
Closed

Tracing in JS is broken #1274

kyzyx opened this issue Dec 29, 2024 · 0 comments · Fixed by #1275

Comments

@kyzyx
Copy link
Contributor

kyzyx commented Dec 29, 2024

Using the traceSync and traceAsync functions in JS cause errors:

  • When wrapping functions that should return a value, the traced version does not return the value.
  • When wrapping functions that should not return a value, the trace is not submitted successfully and an error message is printed.

I used the following test file in a fresh node install

npm init -y
npm install typescript @boundaryml/baml jest ts-jest
npx ts-jest config:init
npx baml-cli init && npx baml-cli generate
npx jest test.ts
// test.ts
import { describe, test, expect } from '@jest/globals';
import { traceSync, traceAsync } from './baml_client/tracing';

function fnWithReturn() : string {
    console.log("fnWithReturn");
    return "Hello, world";
}

function fnWithNoReturn() : void {
    console.log("fnWithNoReturn");
}

async function asyncfnWithReturn() : Promise<string> {
    console.log("asyncfnWithReturn");
    return Promise.resolve("Hello, world");
}

async function asyncfnWithNoReturn() : Promise<void> {
    console.log("asyncfnWithNoReturn");
}


describe('Trace Test', () => {
    test('traceSync with Return', async () => {
        const tracedFnWithReturn = traceSync("tracedFnWithReturn", fnWithReturn);
        const result = tracedFnWithReturn();
        expect(result).toBeDefined();
        expect(result).toBe('Hello, world');
    });
    test('traceSync with no return', async () => {
        const tracedFnWithNoReturn = traceSync("tracedFnWithNoReturn", fnWithNoReturn);
        tracedFnWithNoReturn();
    });
    test('traceAsync with Return', async () => {
        const tracedFnWithReturn = traceAsync("tracedAsyncFnWithReturn", asyncfnWithReturn);
        const result = await tracedFnWithReturn();
        expect(result).toBeDefined();
        expect(result).toBe('Hello, world');
    });
    test('traceAsync with no return', async () => {
        const tracedFnWithNoReturn = traceAsync("tracedAsyncFnWithNoReturn", asyncfnWithNoReturn);
        await tracedFnWithNoReturn();
    });
});

Two tests fail while two other tests appear to pass but print out error messages.

github-merge-queue bot pushed a commit that referenced this issue Dec 30, 2024
Fixes #1274 
<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Fix TypeScript tracing bugs in `BamlCtxManager` by handling undefined
responses and ensuring proper return values.
> 
>   - **Behavior**:
> - Fix `span.finish` in `BamlCtxManager` to handle undefined `response`
by defaulting to an empty object.
> - Ensure `ctx.run` returns the result in `traceFnSync` and
`traceFnAsync` methods of `BamlCtxManager`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 8faf23e. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: aaronvg <[email protected]>
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 a pull request may close this issue.

1 participant