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

Is it possible to save traces locally? #164

Closed
msurguy opened this issue Mar 14, 2025 · 3 comments
Closed

Is it possible to save traces locally? #164

msurguy opened this issue Mar 14, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@msurguy
Copy link

msurguy commented Mar 14, 2025

Describe the feature

Would like to save traces locally for debugging, especially when using custom models. Is it possible to save traces to file(s) locally?

@msurguy msurguy added the enhancement New feature or request label Mar 14, 2025
@rm-openai
Copy link
Collaborator

Yup. Something like this should work:

  1. Create a trace processor that exports to a file
class MyTraceProcessor(TracingProcessor):
    def __init__(self, output_dir: str):
        self._output_dir = output_dir
        self._output_file = open(os.path.join(output_dir, "traces.jsonl"), "w")

    def on_trace_start(self, trace: Trace) -> None:
        pass

    def on_trace_end(self, trace: Trace) -> None:
        self._output_file.write(json.dumps(trace.export()))
        pass

    def on_span_start(self, span: Span[Any]) -> None:
        pass

    def on_span_end(self, span: Span[Any]) -> None:
        self._output_file.write(json.dumps(span.export()))

    def shutdown(self, timeout: float | None = None):
        self._output_file.close()

    def force_flush(self):
        pass

and register it as an additional processor:

from agents import add_trace_processor

add_trace_processor(MyTraceProcessor("output"))

@pontus-devoteam
Copy link

Made a SDK in GO, you could check it out. Allows you to run everything locally.
https://github.com/pontus-devoteam/agent-sdk-go

@vincenzodomina
Copy link
Contributor

Added a pull request #192 for the missing TracingProcessor export. That was necessary for me to make the above example work for me.

Also a little improvement to not throw an error for the missing output dir in case you did not add one like me:

def __init__(self, output_dir: str):
        self._output_dir = output_dir
        # added this to create dir and file if missing
        os.makedirs(output_dir, exist_ok=True)
        self._output_file = open(os.path.join(output_dir, "traces.jsonl"), "w")

rm-openai added a commit that referenced this issue Mar 17, 2025
# Summary
This adds the missing TracingProcessor export to  __init__.py.

# Behavior
When trying to add a custom tracing processor, the TracingProcessor
importing fails with not found error when trying the example usage
proposed in issue #164

Specifically this line throws the error:
`add_trace_processor(MyTracingProcessor("output"))`

# Expected Behavior
Inspecting the init file, simply the import/export was missing. Adding
these made the example code work for me

# Test plan
Local dev of example code in #164

# Issue number
#164

# Checks
None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants