fix: rename runtime_context to request_context to match Mastra body schema - #37
Merged
Merged
Conversation
…chema Mastra's POST /agents/:agentId/generate handler reads `requestContext` from the request body, not `runtimeContext`. The client was emitting `runtime_context:` which `deep_camelize_keys` rewrote to `runtimeContext` on the wire, so the value was silently dropped by Mastra and every dynamic-instructions agent saw `requestContext.get(...)` return `undefined`. Rename the kwarg (and the options hash key) on `Ai::Agent#generate_text` and `#generate_object` to `request_context:` so the camelizer produces `requestContext`, matching the current Mastra schema. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
rogercampos
enabled auto-merge (squash)
April 20, 2026 15:54
rogercampos
disabled auto-merge
April 20, 2026 15:57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mastra's
POST /agents/:agentId/generatehandler readsrequestContextfrom the request body. The gem was emittingruntime_context:on the options hash;deep_camelize_keysrewrote that toruntimeContexton the wire, which Mastra's schema does not recognize — the value was silently dropped. Every caller using dynamic instructions was therefore seeingrequestContext.get(key)returnundefinedand falling back to defaults.Concrete symptoms we hit in
factorialco/factorialbefore tracking this down:categoryHierarchyandcurrentDatenever arrived, so the model saw an empty{}hierarchy and never picked a category.FinanceExpensesPolicyCompliance+FinanceExpensesExpenseApprovalRecommendation:languagenever arrived, output always in English regardless of company locale.TalentEngagementMeetingsAiSummary+TalentEngagementMeetingsContextGenerator:languagenever arrived;template_typealso never arrived (separate TS-side fix because of nested camelization — out of scope here).Origin of the bug
This gem's first commit landed on 2025-06-10 using
runtime_context:, which matched the then-current Mastra API (RuntimeContext).Upstream Mastra renamed the type in mastra-ai/mastra#9511 "Rename RuntimeContext to RequestContext", merged 2025-10-30, and shipped it as a breaking change in
@mastra/core@1.0.0(see the1.0.0"Major Changes" section ofpackages/core/CHANGELOG.md). On the server side the/agents/:agentId/generatehandler was changed to readrequestContextfrom the body (agentExecutionBodySchemaonly declaresrequestContext;runtimeContextis unknown and falls into...rest, whichagent.generatedoes not consume).The gem hadn't been updated in the meantime — we were on
0.5.3still emittingruntimeContext. Since Mastra's Zod schema uses.passthrough(), the unknown key survived validation and was silently dropped by the handler, so no error surfaced and no logs flagged it. The failure mode was "dynamic instructions fall back to defaults on every request", which is very easy to miss when those defaults produce plausible output (English summaries, empty category hierarchy → "other", etc.).factorialco/factorial-agentis already running@mastra/core@1.4.0, so the mismatch has been live for the full life of everyruntime_context:caller in production.Change
Rename the public kwarg on
Ai::Agent#generate_textandAi::Agent#generate_objectfromruntime_context:torequest_context:, and rename the options-hash key sodeep_camelize_keysemitsrequestContext— the key Mastra actually reads.Files touched:
lib/ai/agent.rb— the rename itself.spec/lib/ai/agent_spec.rb— matching spec updates.bin/console+README.md— example code.lib/ai/version.rb— bumped to0.6.0(API rename is a breaking change for callers of this gem).Caller migration
Anywhere that passes
runtime_context:must now passrequest_context:. Infactorialco/factorialthis was five call sites:ocr/.../factorial_agent/ocr_client.rbexpenses/.../generate_policy_flags.rbexpenses/.../generate_approval_recommendation.rbmeetings/.../ai_summary/generator.rbmeetings/.../context/generator.rbThose will be updated in a matching PR against that repo.
Test plan
bundle exec rspec— 83 examples, 0 failures.bundle update aiand restarting Mastra, dynamic instructions are now receiving their context (hierarchy / language / template type / etc.).🤖 Generated with Claude Code