#!/usr/bin/env python3
"""
Generated test file for Basic Error LLM Test
Framework: langchain
Execution mode: async
Streaming: yes"""

import os
import sentry_sdk
import asyncio

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
import respx
import httpx

respx.route(host="api.openai.com").mock(return_value=httpx.Response(500, json={"error": {"message": "Server error", "type": "server_error"}}))
respx.mock.start()

sentry_sdk.init(
    dsn=os.environ.get("SENTRY_DSN"),
    traces_sample_rate=1.0,
    send_default_pii=True,
    stream_gen_ai_spans=True,
)

chat = ChatOpenAI(model="gpt-5-nano")

async def main():
    # Turn 1
    messages = [
        SystemMessage(content="You are a helpful assistant."),
        HumanMessage(content="What is the capital of France?"),
    ]
    chunks = []
    async for chunk in chat.astream(messages):
        chunks.append(chunk.content)
    response_content = "".join(chunks)
    print(f"Turn 1 Response: {response_content}")

if __name__ == "__main__":
    try:
        with sentry_sdk.start_transaction(op="test", name="Basic Error LLM Test"):
            asyncio.run(main())
    except Exception as e:
        print(f"Expected error: {type(e).__name__}: {e}")
        sentry_sdk.capture_exception(e)
    finally:
        sentry_sdk.flush(timeout=5)
