Skip to content

Commit 80a71f6

Browse files
committed
Improve OpenAI API key handling and CI debugging
1. Enhanced CI workflow to verify OpenAI API key availability 2. Added health check verification for Agent Memory Server 3. Fixed notebook to not set dummy OpenAI keys in CI 4. Added script to fix OpenAI key handling in notebooks 5. Added better error messages and logging for debugging This ensures the Agent Memory Server has access to the real OpenAI API key in CI, and notebooks don't override it with dummy values.
1 parent 8ff0982 commit 80a71f6

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ jobs:
109109
env:
110110
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
111111
run: |
112+
# Verify OpenAI API key is available
113+
if [ -z "$OPENAI_API_KEY" ]; then
114+
echo "ERROR: OPENAI_API_KEY is not set!"
115+
exit 1
116+
fi
117+
echo "✅ OpenAI API key is available (length: ${#OPENAI_API_KEY})"
118+
119+
# Start the Agent Memory Server
112120
docker run -d \
113121
--name agent-memory-server \
114122
--network host \
@@ -121,13 +129,21 @@ jobs:
121129
echo "Waiting for Agent Memory Server to be ready..."
122130
for i in {1..30}; do
123131
if curl -f http://localhost:8000/health 2>/dev/null; then
124-
echo "Agent Memory Server is ready!"
132+
echo "Agent Memory Server is ready!"
125133
break
126134
fi
127135
echo "Waiting... ($i/30)"
128136
sleep 2
129137
done
130138
139+
# Verify the server is actually running
140+
if ! curl -f http://localhost:8000/health 2>/dev/null; then
141+
echo "ERROR: Agent Memory Server failed to start!"
142+
echo "Docker logs:"
143+
docker logs agent-memory-server
144+
exit 1
145+
fi
146+
131147
- name: Create and activate venv
132148
run: |
133149
python -m venv venv

python-recipes/context-engineering/notebooks/section-3-memory/01_working_memory_with_extraction_strategies.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
" import getpass\n",
7070
" os.environ[key] = getpass.getpass(f\"{key}: \")\n",
7171
" else:\n",
72-
" # Non-interactive environment (like CI) - use a dummy key\n",
73-
" print(f\"⚠️ Non-interactive environment detected. Using dummy {key} for demonstration.\")\n",
74-
" os.environ[key] = \"sk-dummy-key-for-testing-purposes-only\"\n",
72+
" # Non-interactive environment (like CI)\n",
73+
" print(f\"⚠️ {key} not found in environment. Some features may not work.\")\n",
74+
" pass # Let it fail if key is actually needed\n",
7575
"\n",
7676
"_set_env(\"OPENAI_API_KEY\")\n",
7777
"\n",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Fix OpenAI API key handling in notebooks to use real keys when available.
4+
5+
This script updates notebooks to not set dummy keys in CI environments,
6+
allowing them to use the real OPENAI_API_KEY from the environment.
7+
"""
8+
9+
import json
10+
import sys
11+
from pathlib import Path
12+
13+
14+
def fix_notebook(notebook_path: Path) -> bool:
15+
"""Fix OpenAI key handling in a single notebook."""
16+
print(f"Processing: {notebook_path}")
17+
18+
with open(notebook_path, 'r') as f:
19+
nb = json.load(f)
20+
21+
modified = False
22+
23+
for cell in nb['cells']:
24+
if cell['cell_type'] == 'code':
25+
# Check if this cell has the _set_env function
26+
source_text = ''.join(cell['source'])
27+
if '_set_env' in source_text and 'sk-dummy-key-for-testing-purposes-only' in source_text:
28+
# Replace the dummy key logic
29+
new_source = []
30+
for line in cell['source']:
31+
if 'sk-dummy-key-for-testing-purposes-only' in line:
32+
# Skip setting a dummy key - just pass
33+
new_source.append(' pass # Let it fail if key is actually needed\n')
34+
modified = True
35+
elif '# Non-interactive environment (like CI) - use a dummy key' in line:
36+
new_source.append(' # Non-interactive environment (like CI)\n')
37+
modified = True
38+
elif 'Non-interactive environment detected. Using dummy' in line:
39+
new_source.append(' print(f"⚠️ {key} not found in environment. Some features may not work.")\n')
40+
modified = True
41+
else:
42+
new_source.append(line)
43+
44+
if modified:
45+
cell['source'] = new_source
46+
47+
if modified:
48+
with open(notebook_path, 'w') as f:
49+
json.dump(nb, f, indent=2, ensure_ascii=False)
50+
f.write('\n') # Add trailing newline
51+
print(f" ✅ Updated {notebook_path.name}")
52+
return True
53+
else:
54+
print(f" ⏭️ No changes needed for {notebook_path.name}")
55+
return False
56+
57+
58+
def main():
59+
notebooks_dir = Path(__file__).parent.parent / 'notebooks'
60+
61+
# Find all notebooks in section-3 and section-4
62+
patterns = [
63+
'section-3-memory/*.ipynb',
64+
'section-4-optimizations/*.ipynb'
65+
]
66+
67+
total_updated = 0
68+
69+
for pattern in patterns:
70+
for notebook_path in notebooks_dir.glob(pattern):
71+
if fix_notebook(notebook_path):
72+
total_updated += 1
73+
74+
print(f"\n✅ Updated {total_updated} notebooks")
75+
return 0
76+
77+
78+
if __name__ == '__main__':
79+
sys.exit(main())
80+

0 commit comments

Comments
 (0)